stretchableImageWithLeftCapWidth:topCapHeight 在 UIImageView 子类的 initWithCoder: 中不起作用
我有一个名为 ShadowView 的 UIImageView 子类,它显示可以在任何东西下使用的阴影。 ShadowViews 将从笔尖加载。
在 initWithCoder: 中,我有以下代码:
- (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;
}
但是,当我运行该应用程序时,该图像不会出现。
但如果我把它改成
...
UIImage *shadowImage = [UIImage imageNamed:@"drop_shadow_4_pix.png"];
...
它工作正常,但它被拉伸错误。
关于为什么会发生这种情况有什么想法吗?
编辑:当我以编程方式加载shadowview时,情况是一样的,initWithFrame:的实现与initWithCoder:类似。
另一个编辑:我想我解决了这个问题。 我需要设置自动调整大小蒙版。
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.
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.
But if I change it to
...
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?
Edit: it is the same when I load the shadowview programmatically, with initWithFrame: implemented similarly to initWithCoder:.
Another Edit: I think I solved the problem. I needed to set the autoresizing masks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ShadowImage 为零吗?
如果基础图像小于 5 像素宽或 5 像素高,则该方法可能返回 nil,因为它需要 4 像素用于上限 + 1 像素来拉伸。
Is shadowImage nil?
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.