Iphone 是否更改UIAlertView的背景色?

Iphone 是否更改UIAlertView的背景色?,iphone,objective-c,cocoa-touch,uikit,uialertview,Iphone,Objective C,Cocoa Touch,Uikit,Uialertview,我想更改UIAlertView的背景色,但它似乎没有颜色属性。我不相信有公开的方法或属性可用于此操作。设置UIAlertView的背景色(作为UIView)只会在警报视图后面放置一个彩色矩形背景 我想说,改变这种颜色可能会违反iPhone的通用界面,因此我不认为这是推荐的行为(就像不建议在Mac OS X中更改警报视图的背景色一样)。这是不可能的 您需要创建自己的警报类型视图(包括提供动画等),或者使用Apple提供的默认UIAlertView 苹果倾向于不公开UIAlertView的颜色等内容

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

我不相信有公开的方法或属性可用于此操作。设置UIAlertView的背景色(作为UIView)只会在警报视图后面放置一个彩色矩形背景


我想说,改变这种颜色可能会违反iPhone的通用界面,因此我不认为这是推荐的行为(就像不建议在Mac OS X中更改警报视图的背景色一样)。

这是不可能的

您需要创建自己的警报类型视图(包括提供动画等),或者使用Apple提供的默认UIAlertView


苹果倾向于不公开UIAlertView的颜色等内容,这样UI在所有应用程序中都有共同的感觉。在不同的应用程序之间更改颜色会让用户感到困惑。

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]];

我还发现了一个几乎相同的解决方法,在我的例子中,它工作得更好。使用oxigen way out,我发现屏幕上的效果很差,背景变得模糊。我实现的不是UIAlertView的子类化,而是:

- (void)willPresentAlertView:(UIAlertView *)alertView;
所以…只要复制粘贴就行了

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

将QuartzCore框架添加到应用程序中,并在源文件中包含#import“QuartzCore/CALayer.h”。

我最近需要它,并最终将UIAlertView子类化。这是所有感兴趣的人的代码


我用另一种方式解决了这个问题。我搜索了包含背景图像的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

下面是一个更为近期的解决方案,它利用了块,并以TweetBot为模型:

您必须使用:

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"You are Select Row of" message:@"Human" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        alert.backgroundColor=[UIColor redColor];
        [alert show];
        [alert release];

它将更改UIAlertView的背景色。

这看起来是我认为最可靠的解决方案

只需使用此代码

 UIImage *theImage = [UIImage imageNamed:@"imageName.png"];
 UIView *view=[alert valueForKey:@"_backgroundImageView"];
 //Set frame according to adustable
  UIImageView *image=[[UIImageView alloc] initWithImage:theImage];
 [image setFrame:CGRectMake(0, 0, 280, 130)];
 [view addSubview:image];

我最近发现了GRAlertView,它允许您以一种至少我喜欢的简单方式定制UIAlertView。您可以在这里找到:

这个解决方案很好,因为不需要生成背景图像,但是它看起来不如苹果的版本好。链接不再好了。请务必查看:关于一些改进建议,我可以如何使图像缩放到适合alertview?我尝试了这个方法,但没有效果。现在我使用的是高分辨率的图像。这在iOS 4.2上似乎不再适用了。。。我以前在iOS上用过这个。。。我仍在寻找另一种方法。它仍在我的自定义图像上显示默认警报背景…有什么想法吗?不允许更改颜色将使应用程序开发人员创建自己的UI元素,这可能会导致更加混乱;)没错,但我想如果苹果对我们强加规则,他们也应该遵循类似的规则,对吗?如果你看到GameKit的AlertView,所有这些都是黑色背景色。奇怪!从iOS 5开始,这种方法仍然有效,可以在'-(void)willPresentAlertView:(UIAlertView*)alertView'方法中替换图像,该方法不需要重写'show'方法。我想澄清一下:如果您正在创建UIAlertView的子类,请重写-(void)show。或者您可以在委托中包含willPresentAlertView内部的上述代码。这是该分支的一个分支:警告该类-当设备旋转时,它不知道如何正确显示自己。它也与iOS 3.1.x不兼容。此操作会将背景颜色从clearColor更改为纯色,从而导致外观非常差的UIAlertView。如果回答了您的问题,请标记答案。