StretchableImageWithLeftCapWidth:topCapHeight在UIImageView子类的initWithCoder:中不起作用 [英] stretchableImageWithLeftCapWidth:topCapHeight doesn't work in initWithCoder: of UIImageView subclass
问题描述
我有一个名为ShadowView的UIImageView子类,该子类显示可以在任何情况下使用的阴影.ShadowViews将从笔尖加载.
I have a UIImageView subclass called ShadowView, which displays a shadow that can be used under anything. ShadowViews are to be loaded from a nib.
在initWithCoder:中,我有以下代码:
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 I run the app, though, this image does not appear.
但是如果我将其更改为
...
UIImage *shadowImage = [UIImage imageNamed:@"drop_shadow_4_pix.png"];
...
它工作正常,但被错误拉伸了.
it works fine, but it is stretched wrong.
关于为什么发生这种情况的任何想法吗?
Any ideas as to why this is happening?
编辑:当我以编程方式加载阴影视图时,使用initWithFrame:与initWithCoder:相似地实现,是相同的.
it is the same when I load the shadowview programmatically, with initWithFrame: implemented similarly to initWithCoder:.
另一个我想我已经解决了这个问题.我需要设置自动调整大小的蒙版.
Another I think I solved the problem. I needed to set the autoresizing masks.
推荐答案
shadowImage是否为空?
Is shadowImage nil?
UIImage *shadowImage = [[UIImage imageNamed:@"drop_shadow_4_pix.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:4];
如果基本图像的宽度小于5像素或高度小于5像素,则该方法可能返回nil,因为它需要4个像素的上限和1个像素才能拉伸.
That method could return nil if the base image is less than 5 pixels wide or 5 pixels tall since it needs the 4 pixels for the caps + 1 pixel to stretch.
这篇关于StretchableImageWithLeftCapWidth:topCapHeight在UIImageView子类的initWithCoder:中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!