StretchableImageWithLeftCapWidth: topCapHeight not working in initWithCoder: subclass of UIImageView

I have a subclass of UIImageView called ShadowView that displays a shadow that can be used under any. ShadowViews should be loaded from the tip.

In initWithCoder :, I have the following code:

- (id)initWithCoder:(NSCoder *)decoder {
    self = [super initWithCoder:decoder];
    if (self != nil) {
        UIImage *shadowImage = [[UIImage imageNamed:@"drop_shadow_4_pix.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:4];

        [self setContentMode:UIViewContentModeScaleToFill];
        [self setImage:shadowImage];
    }
    return self;
}

When the application starts, this image does not appear.

But if I change it to

...
UIImage *shadowImage = [UIImage imageNamed:@"drop_shadow_4_pix.png"];
...

It works fine, but it does not stretch properly.

Any ideas as to why this is happening?

Edit: the same when I load the shadowview programmatically, with initWithFrame: implemented similarly to initWithCoder :.

Other Edit: I think I solved the problem. I needed to set the autoresist masks.

+3
1

shadowImage nil?

UIImage *shadowImage = [[UIImage imageNamed:@"drop_shadow_4_pix.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:4];

, 5 5 , 4 + 1 .

+3

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


All Articles