改变UIAlertView的背景色?

时间:2022-03-07 05:52:53

I want to change the background color of my UIAlertView, but this doesn't appear to have a color attribute.

我想要改变UIAlertView的背景颜色,但这看起来没有颜色属性。

12 个解决方案

#1


33  

Background of AlertView is an image And you can change this image

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

#2


12  

I've also find an almost identical workaround and, in my case, it works better. Using oxigen way out i've discovered a poor result on the screen and the context get blurred. Rather than subclassing UIAlertView I implement:

我还找到了一个几乎相同的解决方案,在我的例子中,它工作得更好。使用oxigen的方法,我在屏幕上发现了一个糟糕的结果,上下文变得模糊了。我实现的不是子类化UIAlertView:

- (void)willPresentAlertView:(UIAlertView *)alertView;

so...just copy & paste

所以…只是拷贝和粘贴

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

#3


10  

I recently needed this and ended up subclassing UIAlertView. Here is the code for anyone who is interested.

我最近需要这个,最后子类化UIAlertView。这是给感兴趣的人的密码。

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

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

#4


7  

Well i solved the problem in a different way. I searched for the UIImageView which contains the background image and change the image.

我用另一种方式解决了这个问题。我搜索了包含背景图像的UIImageView并修改了图像。

... well show is maybe not the best solution for altering the view ...

…好的,show可能不是改变视图的最佳解决方案……

@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

#5


4  

Here's a much more recent solution that takes advantage of blocks and is modeled after TweetBot:

这里有一个更近期的解决方案,它利用了块,并模仿了TweetBot:

https://github.com/Arrived/BlockAlertsAnd-ActionSheets

https://github.com/Arrived/BlockAlertsAnd-ActionSheets

#6


3  

This is not something that is possible.

这是不可能的。

Either you need to create your own alert-type view (including providing the animation, etc.) or use the default UIAlertView supplied by Apple.

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

Apple tends to not expose things like the color of the UIAlertView so that the UI has a common feel across all applictions. Changing the color from app to app would be confusing to the user.

苹果倾向于不公开像UIAlertView的颜色这样的东西,这样UI在所有应用程序中都有一个共同的感觉。从一个应用到另一个应用改变颜色会让用户感到困惑。

#7


2  

Add QuartzCore framework to the application and include #import "QuartzCore/CALayer.h" in the source file.

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

#8


1  

This looks like the most robust solution to me

这对我来说是最稳健的解决方案

http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uialertview-custom-graphics/

http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uialertview-custom-graphics/

#9


1  

I recently found the GRAlertView, which allow you to tailor UIAlertView in an easy way that at least I like. You find it here: https://github.com/goncz9/GRAlertView

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

#10


0  

You have to use this:

你必须使用这个:

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

it will changed the background color of UIAlertView.

它将改变UIAlertView的背景色。

#11


0  

Simply use this code,

简单地使用这段代码中,

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

#12


-2  

I don't believe that there is an exposed method or property for doing this. Setting the backgroundColor of the UIAlertView (as a UIView) merely puts a colored rectangular backdrop behind the alert view.

我不相信有公开的方法或属性来做这件事。设置UIAlertView的背景色(作为一个UIView)只会在alert view后面放置一个彩色的矩形背景。

I'd say that it would probably go against the common interface of the iPhone to alter this color, so I don't think it's recommended behavior (in the same way that changing the background color of an alert view in Mac OS X is not recommended).

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

#1


33  

Background of AlertView is an image And you can change this image

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

#2


12  

I've also find an almost identical workaround and, in my case, it works better. Using oxigen way out i've discovered a poor result on the screen and the context get blurred. Rather than subclassing UIAlertView I implement:

我还找到了一个几乎相同的解决方案,在我的例子中,它工作得更好。使用oxigen的方法,我在屏幕上发现了一个糟糕的结果,上下文变得模糊了。我实现的不是子类化UIAlertView:

- (void)willPresentAlertView:(UIAlertView *)alertView;

so...just copy & paste

所以…只是拷贝和粘贴

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

#3


10  

I recently needed this and ended up subclassing UIAlertView. Here is the code for anyone who is interested.

我最近需要这个,最后子类化UIAlertView。这是给感兴趣的人的密码。

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

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

#4


7  

Well i solved the problem in a different way. I searched for the UIImageView which contains the background image and change the image.

我用另一种方式解决了这个问题。我搜索了包含背景图像的UIImageView并修改了图像。

... well show is maybe not the best solution for altering the view ...

…好的,show可能不是改变视图的最佳解决方案……

@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

#5


4  

Here's a much more recent solution that takes advantage of blocks and is modeled after TweetBot:

这里有一个更近期的解决方案,它利用了块,并模仿了TweetBot:

https://github.com/Arrived/BlockAlertsAnd-ActionSheets

https://github.com/Arrived/BlockAlertsAnd-ActionSheets

#6


3  

This is not something that is possible.

这是不可能的。

Either you need to create your own alert-type view (including providing the animation, etc.) or use the default UIAlertView supplied by Apple.

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

Apple tends to not expose things like the color of the UIAlertView so that the UI has a common feel across all applictions. Changing the color from app to app would be confusing to the user.

苹果倾向于不公开像UIAlertView的颜色这样的东西,这样UI在所有应用程序中都有一个共同的感觉。从一个应用到另一个应用改变颜色会让用户感到困惑。

#7


2  

Add QuartzCore framework to the application and include #import "QuartzCore/CALayer.h" in the source file.

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

#8


1  

This looks like the most robust solution to me

这对我来说是最稳健的解决方案

http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uialertview-custom-graphics/

http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uialertview-custom-graphics/

#9


1  

I recently found the GRAlertView, which allow you to tailor UIAlertView in an easy way that at least I like. You find it here: https://github.com/goncz9/GRAlertView

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

#10


0  

You have to use this:

你必须使用这个:

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

it will changed the background color of UIAlertView.

它将改变UIAlertView的背景色。

#11


0  

Simply use this code,

简单地使用这段代码中,

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

#12


-2  

I don't believe that there is an exposed method or property for doing this. Setting the backgroundColor of the UIAlertView (as a UIView) merely puts a colored rectangular backdrop behind the alert view.

我不相信有公开的方法或属性来做这件事。设置UIAlertView的背景色(作为一个UIView)只会在alert view后面放置一个彩色的矩形背景。

I'd say that it would probably go against the common interface of the iPhone to alter this color, so I don't think it's recommended behavior (in the same way that changing the background color of an alert view in Mac OS X is not recommended).

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