更改UIAlertView的背景颜色?

Ais*_*rya 38 iphone cocoa-touch objective-c uikit uialertview

我想更改UIAlertView的背景颜色,但这似乎没有颜色属性.

oxi*_*gen 34

AlertView的背景是图像您可以更改此图像

UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:@"Atention"
   message: @"YOUR MESSAGE HERE", nil)
   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];

   [theAlert show];

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

   UILabel *theBody = [theAlert valueForKey:@"_bodyTextLabel"];
   [theBody setTextColor:[UIColor blueColor]];

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

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

   [[theAlert layer] setContents:[theImage CGImage]];
Run Code Online (Sandbox Code Playgroud)

  • 这似乎不再适用于iOS 4.2 ...我一直在早期的iOS上使用它...我仍然找到另一种方式. (3认同)

mar*_*cio 13

我也发现了一个几乎相同的解决方法,在我看来,它的效果更好.使用oxigen方式我发现屏幕上的结果很差,上下文变得模糊.而不是子类化UIAlertView我实现:

- (void)willPresentAlertView:(UIAlertView *)alertView;
Run Code Online (Sandbox Code Playgroud)

所以...只需复制和粘贴

- (void)willPresentAlertView:(UIAlertView *)alertView 
{
    blah blah blah...
    [[alertView layer] setContents:(id)theImage.CGImage];  // to avoid the warning
}
Run Code Online (Sandbox Code Playgroud)


小智 10

我最近需要这个并最终继承UIAlertView的子类.以下是感兴趣的人的代码.

http://kwigbo.com/post/318396305/iphone-sdk-custom-uialertview-background-color

  • 链接坏了. (2认同)

小智 7

我用不同的方式解决了这个问题.我搜索了包含背景图像的UIImageView并更改了图像.

...好的节目可能不是改变观点的最佳解决方案......

@implementation CustomAlertView

- (void)show {
    [super show];

    for (UIView *view in self.subviews) {
        NSLog(@"%@ with %i children", view, [view.subviews count]);
        // change the background image
        if ([view isKindOfClass:[UIImageView class]]) {
            UIImage *theImage = [UIImage imageNamed:@"password entry bg - default.png"];    
            ((UIImageView *)view).image = [theImage resizableImageWithCapInsets:UIEdgeInsetsMake(49, 8, 8, 8)];
        }
    }
}

@end
Run Code Online (Sandbox Code Playgroud)