Subclassified UIAlertView not displaying properly in iOS 4.2

I have subclassed UIAlertView as follows:

@interface NarrationAlertView : UIAlertView {
    UIImage * backgroundImage; //The image I want as custom background
    UILabel * textualNarrationView; //The test I wanna be displayed on the view        
}

And implemented as follows:

- (id)initNarrationViewWithImage:(UIImage *)image{

    if (self = [super init]){
        UILabel * alertTextLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.textualNarrationView = alertTextLabel;
        [alertTextLabel release];
        [self addSubview:alertTextLabel];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    /* Here I draw the image as background */
    CGSize imageSize = self.backgroundImage.size;
    [self.backgroundImage drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
}

- (void)layoutSubviews {
    /* To fit the text */
    [textualNarrationView sizeToFit];

    CGRect textRect = textualNarrationView.frame;
    textRect.origin.x = (CGRectGetWidth(self.bounds) - CGRectGetWidth(textRect)) / 2;
    textRect.origin.y = (CGRectGetHeight(self.bounds) - CGRectGetHeight(textRect)) / 2;
    textRect.origin.y -= 70;
    textualNarrationView.frame = textRect;
}

- (void)show{
    /* Showing the view */
    [super show];
    CGSize imageSize = self.backgroundImage.size;
    self.bounds = CGRectMake(0, 0, imageSize.width, imageSize.height);
}

In previous versions of iOS (I always tested on the simulator), the subclass worked fine, and when it was shown, it displayed a correctly configured background image and only text , whereas in 4.2, it draws the classic UIAlertView background (blue rounded rectangle) on top of mine Images.

What am I doing wrong? Any suggestion on programming UIAlertView and UIView will also be appreciated.

UPDATE . Does anyone have a UIAlertView replacement class to share?

+2
3

UIAlertView iOS 4.2; , .

, - UIAlertView . , , / UIAlertView , 4.2 .

, UIAlertView.

+5

iOS 4.2. drawRect UIAlertView. UIAlertView drawRect super. 4.2 UIImageView. , ( ) . UIImageView UIAlertView, UIImageView , , subview:

- (void)didAddSubview:(UIView *)subview {
  if ([subview isMemberOfClass:[UIImageView class]]) {
    [subview removeFromSuperview];
  }
}
+3

They burned me too. I ended up writing a replacement class, which I share on github:

https://github.com/TomSwift/TSAlertView

+1
source

Source: https://habr.com/ru/post/1776758/


All Articles