警告框大小更改问题

9

你好,我有一个警示框,我想通过cgrectmake属性更改其大小,但是它没有发生变化。它只采用默认的大小。

我尝试了以下代码。

- (void)viewDidLoad {
[super viewDidLoad];
UIAlertView* find = [[[UIAlertView alloc] init]initWithFrame:CGRectMake(0,0,300,200)];
[find setDelegate:self];
[find setTitle:@"Sample Alert"];
[find setNeedsLayout];
[find show];
[find release];

谢谢您提前阅读。


我也有相同的问题。我无法更改UIAlertView的宽度和高度。有什么帮助吗?提前致谢。 - Sakthimuthiah
4个回答

16
为了达到你想要的目标,在你的代码中,在以下代码之后:
[find show];

添加:

find.frame = CGRectMake(0,0,300,200);

不过并不美观,建议使用ActionSheets。


这里我想要改变警告视图的宽度,我应该怎么改呢?我已经能够改变高度了,但是不知道如何改变宽度。 - mrugen munshi
我的回答仍然适用,你可以在调用[find show]之后更改宽度,但结果仍然很丑陋,你应该仍然使用操作表。 - ahmet emrah
这只是为我更改了UIWindow(即内部的文本字段“message”没有调整大小) - roocell
@rocell 我有类似的问题。子视图没有正确地调整大小。 - johnnieb
@ahmetemrah。定制警告视图。这在苹果规定中可以吗?我的意思是,关于审批或拒绝方面怎么样?我能自定义警告视图吗? - Gajendra K Chauhan

7
这个方法做得很好,当警报出现时不会显得奇怪(Ahmet Emrah的解决方案有一个相当严重的副作用)。
CGAffineTransform myTransform = CGAffineTransformMakeScale(1.0, 0.5f);
[alert setTransform:myTransform];

不行。这会缩小警报视图的内容,扭曲文本和其他所有内容。 - AWrightIV

7
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirmation" message:@" Submit the answer " delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[alert show];
[alert release];

UILabel *theTitle = [alert valueForKey:@"_titleLabel"];
[theTitle setTextColor:[UIColor blackColor]];

UILabel *theBody = [alert valueForKey:@"_bodyTextLabel"];
[theBody setTextColor:[UIColor blackColor]];

UIImage *theImage = [UIImage imageNamed:@"blue-white-abstract-background.jpg"];    
theImage = [theImage stretchableImageWithLeftCapWidth:10 topCapHeight:10];
CGSize theSize = [alert frame].size;

UIGraphicsBeginImageContext(theSize);    
[theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];    
theImage = UIGraphicsGetImageFromCurrentImageContext();    
UIGraphicsEndImageContext();

//[[alert layer] setContents:[theImage CGImage]];
[[alert layer] setContents:[UIColor clearColor]];

这段代码对alertview做了很多操作,将其修改以增加警告视图的大小。


3
你可以对UIAlertView进行子类化。我已经做过类似的事情,根据你的需求进行修改。
头文件:
#import <Foundation/Foundation.h>

/* An alert view with a textfield to input text.  */
@interface AlertPrompt : UIAlertView    
{
    UITextField *textField;
}

@property (nonatomic, retain) UITextField *textField;
@property (readonly) NSString *enteredText;

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle;

@end

源代码,

#import "AlertPrompt.h"


@implementation AlertPrompt

static const float kTextFieldHeight     = 25.0;
static const float kTextFieldWidth      = 100.0;

@synthesize textField;
@synthesize enteredText;

- (void) drawRect:(CGRect)rect {
    [super drawRect:rect];

    CGRect labelFrame;
    NSArray *views = [self subviews];
    for (UIView *view in views){
        if ([view isKindOfClass:[UILabel class]]) {
            labelFrame = view.frame;
        } else {    
            view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y + kTextFieldHeight , view.frame.size.width, view.frame.size.height);
        }

    }

    CGRect myFrame = self.frame;
    self.textField.frame = CGRectMake(95, labelFrame.origin.y+labelFrame.size.height + 5.0, kTextFieldWidth, kTextFieldHeight);
    self.frame = CGRectMake(myFrame.origin.x, myFrame.origin.y, myFrame.size.width, myFrame.size.height + kTextFieldHeight);

}

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle
{

    if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
    {
        // add the text field here, so that customizable from outside. But set the frame in drawRect. 
        self.textField = [[UITextField alloc] init];
        [self.textField setBackgroundColor:[UIColor whiteColor]]; 
        [self addSubview: self.textField];

       // CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 20.0); 
      //  [self setTransform:translate];
    }
    return self;
}
- (void)show
{
    [textField becomeFirstResponder];
    [super show];
}
- (NSString *)enteredText
{
    return textField.text;
}
- (void)dealloc
{
    [textField release];
    [super dealloc];
}
@end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接