简体   繁体   English

自定义UI警报视图无法正确显示

[英]Custom UI Alert View not displaying correctly

Im creating a custom ui alert view. 我正在创建自定义ui警报视图。 All works ok except any background image that is used for the custom alert has the standard blue overlay over the top of it. 一切正常,除了用于自定义警报的任何背景图像上方都带有标准的蓝色叠加层。 Anyway I can get rid of this? 无论如何我都可以摆脱吗?

(the telephone is the backgroundImage for the alert view) (电话是警报视图的backgroundImage

在此处输入图片说明

.m of custom class .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);
}

Called in my Viewcontroller for the alert 在我的Viewcontroller中调用该警报

IBAction: 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];

AS per the ios documentation 根据ios文档的AS

Subclassing Notes 子类注释

The UIAlertView class is intended to be used as-is and does not support subclassing. UIAlertView类旨在按原样使用,并且不支持子类化。 The view hierarchy for this class is private and must not be modified. 此类的视图层次结构是私有的,不能修改。

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

This is probably why you are having trouble modifying the background. 这可能就是为什么您在修改背景时遇到麻烦。

Anyways, What you are looking to do is a simple custom view. 无论如何,您想要做的是一个简单的自定义视图。 Make your own customized view and make it behave like an alert view yourself. 制作自己的自定义视图,并使自己的行为类似于警报视图。

It is very easy. 这很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM