Search code examples
ioscocoa-touchuiimageviewuiimageviewdidload

Setting resizable UIImage to a UIImageView at viewDidLoad is ignored


I have a UIImageView with an image that I want to be stretchable. In order to achieve that I do the following in viewDidLoad:

-(void) buildResizableDescriptionAreaBackground
{
    UIImage *image = [UIImage imageNamed:@"description_area_background"];
    CGFloat top = ceilf(image.size.height / 2.0) - 1;
    CGFloat bottom = floorf(image.size.height / 2.0);
    CGFloat left = ceilf(image.size.height / 2.0) - 1;
    CGFloat right = floorf(image.size.height / 2.0);
    if ([image respondsToSelector:@selector(resizableImageWithCapInsets:)])
    {
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right)];
    }
    else 
    {
        image = [image stretchableImageWithLeftCapWidth:left topCapHeight:top];
    }
    [self.descriptionAreaBackgroundImage setImage:image];
    [self.descriptionAreaBackgroundImage setHighlightedImage:image];
}

When I debug this I make sure that the descriptionAreaBackgroundImage is not nil, not hidden, alpha = 1 and with the right frame. Also I make sure that the image is not nil, have the right size and that the resizable image is also not nil. And yet the image is not set to the UIImageView.

If I do this at viewDidAppear: everything works fine. If I set a regular image (Not stretchable, even at viewDidLoad) everything works fine.

As far as I know any setup that does not require superview or window can be done at viewDidLoad, so why isn't it working?


Solution

  • OK, I found what causing it, stupid mistake.

    In my viewDidLoad, I'm loading another .xib file containing additional views:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Some setups
        [[NSBundle mainBundle] loadNibNamed:@"RatingDialog" owner:self options:nil];
        // More setups...
        [self buildResizableDescriptionAreaBackground]; // NOT WORKING!!
    }
    

    Apparently, one of the UIImageViews in that nib was also connected to my descriptionAreaBackground outlet by mistake. So when I set the resizable image, it was to the wrong view...

    How dumb.