0
我创建自定义UI警报视图。除了任何用于自定义警报的背景图像都具有标准的蓝色覆盖层外,所有工作都可以正常工作。无论如何,我可以摆脱这一点?自定义UI警报视图显示不正确
(电话是backgroundImage
的警报视图)
的.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];