0

Im creating a custom ui alert view. 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)

enter image description here

.m of custom class

     @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

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];
JSA986
  • 5,870
  • 9
  • 45
  • 91

1 Answers1

1

AS per the ios documentation

Subclassing Notes

The UIAlertView class is intended to be used as-is and does not support subclassing. 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

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.

Pochi
  • 13,391
  • 3
  • 64
  • 104