Iphone 更改uialertview大小时出现问题

Iphone 更改uialertview大小时出现问题,iphone,xcode,sdk,uialertview,Iphone,Xcode,Sdk,Uialertview,你好 我有一个alertview,我想通过cgrectmake属性更改大小,但没有发生。 它只是采用默认大小 我尝试了以下代码 - (void)viewDidLoad { [super viewDidLoad]; UIAlertView* find = [[[UIAlertView alloc] init]initWithFrame:CGRectMake(0,0,300,200)]; [find setDelegate:self]; [find setTitle:@"Sample Alert"]

你好 我有一个alertview,我想通过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];
}


提前感谢。

要在代码中实现所需,请在以下步骤之后:

[find show];
加:


不过这并不漂亮,我建议您使用ActionSheets。

您可以对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

这段代码对alertview做了很多事情,并对其进行了修改以增加alertview的大小。

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


在这里,我想更改警报视图的宽度如何更改,我可以更改高度,但不知道如何更改宽度。我的答案仍然适用,您可以在调用[find show]后更改宽度,结果仍然很糟糕,您应该仍然使用ActionSheets这只是为我更改uiwindow(即-内部文本字段“消息”不调整大小)@rocell我也有一个类似的问题。子视图不能正确调整大小。@ahmetemrah。自定义alertview。这符合苹果的规则吗?我的意思是批准或拒绝。我可以自定义alert view吗?不可以。这会缩小alert view的内容,扭曲文本和所有内容。对我来说是同一个问题。我无法同时更改这两个widUIAlertView的高度和高度。有什么帮助吗?提前谢谢
#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
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]];
CGAffineTransform myTransform = CGAffineTransformMakeScale(1.0, 0.5f);
[alert setTransform:myTransform];