【问题标题】:Changing the background color of a UIAlertView?更改 UIAlertView 的背景颜色?
【发布时间】:2010-10-27 08:39:29
【问题描述】:

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

【问题讨论】:

  • 如果您的问题得到回答,请标记答案。

标签: iphone objective-c cocoa-touch uikit uialertview


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    只需使用此代码,

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

    【讨论】:

      【解决方案3】:

      【讨论】:

        【解决方案4】:

        你必须使用这个:

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

        【讨论】:

        • 这会将背景色从 clearColor 更改为纯色,从而导致 UIAlertView 看起来很糟糕。
        【解决方案5】:

        这是一个更新的解决方案,它利用了块并以 TweetBot 为模型:

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

        【讨论】:

        【解决方案6】:

        好吧,我以不同的方式解决了这个问题。我搜索了包含背景图像的 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
        

        【讨论】:

        • 这种方法在 iOS 5 中仍然有效,图像的替换可以在 '- (void)willPresentAlertView:(UIAlertView *)alertView' 方法中完成,不需要覆盖'show' 方法。
        • 只是想我会澄清一下:如果您正在创建 UIAlertView 的子类,请覆盖 -(void)show。或者您可以将上述代码包含在委托中的 willPresentAlertView 中。
        【解决方案7】:

        我还找到了一个几乎相同的解决方法,就我而言,它效果更好。使用 oxigen 出路我发现屏幕上的结果很差,并且上下文变得模糊。而不是子类化 UIAlertView 我实现:

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

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

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

        【讨论】:

          【解决方案8】:

          我最近需要这个并最终继承了 UIAlertView。这是任何有兴趣的人的代码。

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

          【讨论】:

          • 这个解决方案很好,因为不需要生成背景图像,但是它看起来不如 Apple 制作的版本。
          【解决方案9】:

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

          【讨论】:

            【解决方案10】:

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

            【讨论】:

            • 请务必查看:stackoverflow.com/questions/883208/… 以获得一些改进建议
            • 如何使图像缩放直到适合警报视图??
            • 我试过了,但没用。对图像大小有要求吗,因为我现在使用的是高分辨率图像。
            • 这似乎不再适用于 iOS 4.2...我一直在早期的 iO​​S 上使用它...我仍在寻找另一种方法。
            • 它仍然在我的自定义图像上显示默认警报背景...有什么想法吗?
            【解决方案11】:

            这是不可能的。

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

            Apple 倾向于不公开诸如 UIAlertView 的颜色之类的东西,以便 UI 在所有应用程序中都有一个共同的感觉。将颜色从一个应用更改为另一个应用会让用户感到困惑。

            【讨论】:

            • 不允许更改颜色会使应用程序开发人员创建自己的 UI 元素,最终可能会更加混乱 ;)
            【解决方案12】:

            我不相信有一个公开的方法或属性可以做到这一点。设置 UIAlertView 的 backgroundColor(作为 UIView)只是在警报视图后面放置一个彩色矩形背景。

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

            【讨论】:

            • 是的,但我想如果 Apple 对我们强加规则,他们也应该遵循类似的规则,对吗?如果您看到 GameKit 的 AlertViews,所有这些都是黑色背景色。奇怪!
            猜你喜欢
            • 2013-05-18
            • 2013-08-08
            • 1970-01-01
            • 1970-01-01
            • 2012-04-21
            • 2017-08-11
            相关资源
            最近更新 更多