【问题标题】:problem in changing size of uialertview改变uialertview大小的问题
【发布时间】:2011-04-27 21:11:18
【问题描述】:

你好 我有一个警报视图,我想通过 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 的宽度和高度。有什么帮助吗?提前致谢

标签: iphone xcode sdk uialertview


【解决方案1】:

这很好地完成了这项工作,并且在出现警报时看起来并不奇怪(ahmet emrah 解决方案有一个非常糟糕的副作用)。

CGAffineTransform myTransform = CGAffineTransformMakeScale(1.0, 0.5f);
[alert setTransform:myTransform];

【讨论】:

  • 不。这会缩小警报视图的内容,扭曲文本和所有内容。
【解决方案2】:
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做了很多事情并修改它以增加alert view的大小。

【讨论】:

    【解决方案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
    

    【讨论】:

      【解决方案4】:

      为了实现你想要的,在你的代码之后:

      [find show];
      

      添加:

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

      虽然不是很漂亮,但我建议你使用 ActionSheets。

      【讨论】:

      • 这里我想更改警报视图的宽度,我该如何更改,我可以更改高度但不知道如何更改宽度。
      • 好吧,我的回答仍然适用,您可以在调用 [find show] 后更改宽度,结果仍然很难看,您应该仍然使用操作表
      • 这只是为我改变了 uiwindow(即 - 里面的文本字段“消息”不会调整大小)
      • @rocell 我也有类似的问题。子视图未正确调整大小。
      • @ahmetemrah。定制alertview。苹果规则可以吗?我的意思是批准或拒绝。我可以自定义警报视图吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      相关资源
      最近更新 更多