iphone UIAlertView - 自定义背景颜色

时间:2010-12-13 18:50:35

标签: iphone ios4 uialertview

我需要自定义UIAlertView的背景颜色。

我有一个在iphone 3.x上完美运行的示例代码,但在iphone 4上,alertview显示默认的蓝色。

    UIAlertView *alertView = 
 [[[UIAlertView alloc] initWithTitle:@"Alerta"
        message:msg 
          delegate:nil
       cancelButtonTitle:@"OK" 
       otherButtonTitles:nil] autorelease];

 [alertView show];

 UILabel *theTitle = [alertView valueForKey:@"_titleLabel"];
 [theTitle setTextColor:[UIColor redColor]];

 UILabel *theBody = [alertView valueForKey:@"_bodyTextLabel"];
 [theBody setTextColor:[UIColor whiteColor]];

 UIImage *theImage = [UIImage imageNamed:@"Background.png"];
 theImage = [theImage stretchableImageWithLeftCapWidth:0 topCapHeight:0];
 CGSize theSize = [alertView frame].size;

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

 [[alertView layer] setContents:(id)theImage.CGImage];
[alertView show];

1 个答案:

答案 0 :(得分:1)

尽管最有可能,但不建议修改此类内容,因为简单的更新可能会破坏它。相反,尝试制作自己的警报视图,如Nathan S所示:

How can I customize an iOS alert view?