我正在创建一个自定义 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 中调用警报
IBA动作:
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];