我有一個應用程序,我使用UIAlertView顯示警報消息。默認情況下,對話框的顏色爲藍色。誰能告訴我,如何改變顏色?它可以改變嗎?如何更改iOS中UIAlertView對話框的顏色?
1
A
回答
9
使用這樣
[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
0
您必須執行您的UIAlertView。您不能更改默認的藍色。
4
的UIAlertView中類旨在被原樣使用,並且不支持子類。該類的視圖層次結構是私有的,不能修改。
0
使用一個UIView來顯示,而不是使用然後UIAlertView中通過這個[先前所討論的問題] [1] [1]改變視圖相應
相關問題
- 1. 如何改變對話框的顏色
- 2. 如何更改UIAlertView文字顏色?
- 3. 如何更改對話框的邊框顏色?
- 4. 如何更改jquery ui中的對話框標題顏色?
- 5. 如何在tabcontrol中更改對話框背景顏色?
- 6. 如何更改PreferenceFragment警報對話框的分隔線顏色
- 7. 如何更改Android對話框背後的顏色
- 8. 如何更改對話框的標題背景顏色?
- 9. 如何更改對話框的標題顏色?
- 10. 如何更改日曆對話框的顏色Android
- 11. 如何使用python對話框更改tui的背景顏色
- 12. 如何更改默認片段對話框的主題顏色
- 13. 如何更改對話框的背景覆蓋顏色
- 14. 如何更改Android DatePicker對話框的「分隔線」顏色
- 15. 如何更改進度對話框的背景顏色
- 16. 如何更改支持包的偏好對話框顏色?
- 17. 如何更改DatePicker對話框的顏色
- 18. 如何更改單選警報對話框的顏色?
- 19. 如何更改進度對話框的背景顏色?
- 20. 如何更改jquery ui對話框的背景顏色
- 21. 如何更改按鈕的顏色在警報對話框
- 22. 如何更改JQueryUI對話框標題的顏色?
- 23. 更改Android中對話框標題周圍框架的顏色
- 24. 在Win32中,如何使用「更改顏色」對話框來更改STATIC文本?
- 25. 如何更改顏色對話框語言
- 26. 如何以編程方式更改對話框背景顏色?
- 27. 如何更改默認顏色方案timepicker對話框片段?
- 28. 更改UIAlertView的字體顏色
- 29. 更改UIAlertView的背景顏色?
- 30. 如何更改eclipse中黑色對焦框的顏色?
圍棋的顏色警報:http://stackoverflow.com /Items/883208/change-the- background-color-of-a-uialertview – 2012-03-30 09:26:54