2010-12-13 99 views
0

我需要自定义UIAlertView的背景颜色。iphone 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

虽然这是最有可能的可能,但不建议修改此类型的东西,作为一个简单的更新可能会打破它。相反,请尝试使自己的警报视图由Nathan S很好地演示:

How can I customize an iOS alert view?