如何在 iOS 中更改 UIAlertView 对话框的颜色?
Posted
技术标签:
【中文标题】如何在 iOS 中更改 UIAlertView 对话框的颜色?【英文标题】:How to change color of the UIAlertView dialog in iOS? 【发布时间】:2012-04-13 23:19:07 【问题描述】:我有一个使用 UIAlertView 显示警报消息的应用程序。默认情况下,对话框的颜色为蓝色。谁能告诉我,如何改变它的颜色?可以改吗?
【问题讨论】:
浏览这个[以前讨论过的问题][1] [1]:***.com/questions/883208/… 【参考方案1】:这样使用
[CustomAlert setBackgroundColor:[UIColor blueColor]
withStrokeColor:[UIColor greenColor]];
.h 和 .m 文件的内容
CustomAlert.h
#import <UIKit/UIKit.h>
@interface CustomAlert : UIAlertView
+ (void) setBackgroundColor:(UIColor *) background
withStrokeColor:(UIColor *) stroke;
@end
CustomAlert.m
#import "CustomAlert.h"
@interface CustomAlert (Private)
- (void) drawRoundedRect:(CGRect) rect inContext:(CGContextRef)
context withRadius:(CGFloat) radius;
@end
static UIColor *fillColor = nil;
static UIColor *borderColor = nil;
@implementation CustomAlert
+ (void) setBackgroundColor:(UIColor *) background
withStrokeColor:(UIColor *) stroke
if(fillColor != nil)
[fillColor release];
[borderColor release];
fillColor = [background retain];
borderColor = [stroke retain];
- (id)initWithFrame:(CGRect)frame
if((self = [super initWithFrame:frame]))
if(fillColor == nil)
fillColor = [[UIColor blackColor] retain];
borderColor = [[UIColor colorWithHue:0.625
saturation:0.0 brightness:0.8 alpha:0.8]
retain];
return self;
- (void)layoutSubviews
for (UIView *sub in [self subviews])
if([sub class] == [UIImageView class] && sub.tag == 0)
// The alert background UIImageView tag is 0,
// if you are adding your own UIImageView's
// make sure your tags != 0 or this fix
// will remove your UIImageView's as well!
[sub removeFromSuperview];
break;
- (void)drawRect:(CGRect)rect
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetAllowsAntialiasing(context, true);
CGContextSetLineWidth(context, 0.0);
CGContextSetAlpha(context, 0.8);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [borderColor CGColor]);
CGContextSetFillColorWithColor(context, [fillColor CGColor]);
// Draw background
CGFloat backOffset = 2;
CGRect backRect = CGRectMake(rect.origin.x + backOffset,
rect.origin.y + backOffset,
rect.size.width - backOffset*2,
rect.size.height - backOffset*2);
[self drawRoundedRect:backRect inContext:context withRadius:8];
CGContextDrawPath(context, kCGPathFillStroke);
// Clip Context
CGRect clipRect = CGRectMake(backRect.origin.x + backOffset-1,
backRect.origin.y + backOffset-1,
backRect.size.width - (backOffset-1)*2,
backRect.size.height - (backOffset-1)*2);
[self drawRoundedRect:clipRect inContext:context withRadius:8];
CGContextClip (context);
//Draw highlight
CGGradientRef glossGradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 2;
CGFloat locations[2] = 0.0, 1.0 ;
CGFloat components[8] = 1.0, 1.0, 1.0, 0.35, 1.0, 1.0, 1.0, 0.06 ;
rgbColorspace = CGColorSpaceCreateDeviceRGB();
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace,
components, locations, num_locations);
CGRect ovalRect = CGRectMake(-130, -115, (rect.size.width*2),
rect.size.width/2);
CGPoint start = CGPointMake(rect.origin.x, rect.origin.y);
CGPoint end = CGPointMake(rect.origin.x, rect.size.height/5);
CGContextSetAlpha(context, 1.0);
CGContextAddEllipseInRect(context, ovalRect);
CGContextClip (context);
CGContextDrawLinearGradient(context, glossGradient, start, end, 0);
CGGradientRelease(glossGradient);
CGColorSpaceRelease(rgbColorspace);
- (void) drawRoundedRect:(CGRect) rrect inContext:(CGContextRef) context
withRadius:(CGFloat) radius
CGContextBeginPath (context);
CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect),
maxx = CGRectGetMaxX(rrect);
CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect),
maxy = CGRectGetMaxY(rrect);
CGContextMoveToPoint(context, minx, midy);
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
CGContextClosePath(context);
- (void)dealloc
[super dealloc];
@end
【讨论】:
来自 Apple,The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
,您确定您的子类在所有情况下都能正常工作吗?你有没有在 App Store 上找到一个使用这个类的应用程序?
请使用 UIAlertView 的其他替代品,即使 UIAlertview 早就被弃用了。【参考方案2】:
来自UIAlertView Class Reference:
UIAlertView 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。
【讨论】:
【参考方案3】:你必须实现你的UIAlertView。您无法更改默认的蓝色。
【讨论】:
【参考方案4】:使用 uiview 来显示警报而不是使用 UIAlertView 然后相应地更改该视图的颜色
【讨论】:
以上是关于如何在 iOS 中更改 UIAlertView 对话框的颜色?的主要内容,如果未能解决你的问题,请参考以下文章
应用访问地图服务时系统弹出UIAlertView,可以更改UIAlertView的消息吗?
UIAlertView 已弃用:首先在 iOS 9.0 中弃用 - UIAlertView 已弃用。将 UIAlertController 与首选样式一起使用