objective-c 更改 UIAlertView 的背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/883208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:34:31  来源:igfitidea点击:

Changing the background color of a UIAlertView?

iphoneobjective-ccocoa-touchuikituialertview

提问by Aishwarya

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

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

回答by oxigen

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

回答by marcio

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
}

回答by kwigbo

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

回答by informatics

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 ...

......好吧,展示可能不是改变视图的最佳解决方案......

@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

回答by esilver

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

回答by August

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.

您需要创建自己的警报类型视图(包括提供动画等)或使用 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.

Apple 倾向于不公开 UIAlertView 的颜色之类的东西,以便 UI 在所有应用程序中具有共同的感觉。在应用程序之间更改颜色会使用户感到困惑。

回答by Vijay Shankar

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

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

回答by USK

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 的背景颜色。

回答by Luke

回答by PeterK

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