2012-08-09 80 views
0

我创建自定义UI警报视图。除了任何用于自定义警报的背景图像都具有标准的蓝色覆盖层外,所有工作都可以正常工作。无论如何,我可以摆脱这一点?自定义UI警报视图显示不正确

(电话是backgroundImage的警报视图)

enter image description here

的.m自定义类的

 @synthesize backgroundImage, alertText; 

- (id)initWithImage:(UIImage *)image text:(NSString *)text { 
if (self = [super init]) { 
    alertTextLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
    alertTextLabel.textColor = [UIColor whiteColor]; 
    alertTextLabel.backgroundColor = [UIColor clearColor]; 
    alertTextLabel.font = [UIFont boldSystemFontOfSize:28.0]; 
    [self addSubview:alertTextLabel]; 

    self.backgroundImage = image; 
    self.alertText = text; 
    } 
return self; 
} 

- (void) setAlertText:(NSString *)text { 
alertTextLabel.text = text; 
} 

- (NSString *) alertText { 
return alertTextLabel.text; 
} 


- (void)drawRect:(CGRect)rect { 
//CGContextRef ctx = UIGraphicsGetCurrentContext(); 

CGSize imageSize = self.backgroundImage.size; 
//CGContextDrawImage(ctx, CGRectMake(0, 0, imageSize.width, imageSize.height), self.backgroundImage.CGImage); 
[self.backgroundImage drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)]; 
} 

- (void) layoutSubviews { 
alertTextLabel.transform = CGAffineTransformIdentity; 
[alertTextLabel sizeToFit]; 

CGRect textRect = alertTextLabel.frame; 
textRect.origin.x = (CGRectGetWidth(self.bounds) - CGRectGetWidth(textRect))/2; 
textRect.origin.y = (CGRectGetHeight(self.bounds) - CGRectGetHeight(textRect))/2; 
textRect.origin.y -= 0.0; 

alertTextLabel.frame = textRect; 

alertTextLabel.transform = CGAffineTransformMakeRotation(- M_PI * .08); 
} 

- (void) show { 
[super show]; 

CGSize imageSize = self.backgroundImage.size; 
self.bounds = CGRectMake(0, 0, imageSize.width, imageSize.height); 
} 

在我的viewController呼吁警惕

IBAction为:

UIImage *backgroundImage = [UIImage imageNamed:@"Telephone.png"]; 
alert = [[JKCustomAlert alloc] initWithImage:backgroundImage  text:NSLocalizedString(@"Title", nil)]; 
[alert show]; 


[self performSelector:@selector(hideAlert) withObject:nil afterDelay:4.0]; 

回答

1

AS每IOS文档

子类票据

的UIAlertView中类旨在被原样使用,并且不支持 子类。该类的视图层次结构是私有的,并且不得修改 。

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html

这可能是为什么您无法修改的背景。

无论如何,你正在做的是一个简单的自定义视图。制作您自己的自定义视图,并使其像自己的警报视图一样。

这很容易。