UIView 阴影和 InterfaceBuilder
我想使用 CALayer 添加阴影到 UI(Image)View。 以下代码通常工作正常
previewImage.layer.shadowColor = [[UIColor blackColor] CGColor];
previewImage.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
previewImage.layer.shadowOpacity = 1.0f;
previewImage.layer.shadowRadius = 8.0f;
但是,只有当我以编程方式创建该视图并将其作为子视图添加到我的主视图中时,这才有效。当该视图在 InterfaceBuilder 中设置并定义为 IBOutlet UIImageView 时,这不起作用。没有影子出现。 那么我在这里缺少什么?
I want to add a drop shadow using CALayer to a UI(Image)View.
The following code works generally fine
previewImage.layer.shadowColor = [[UIColor blackColor] CGColor];
previewImage.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
previewImage.layer.shadowOpacity = 1.0f;
previewImage.layer.shadowRadius = 8.0f;
However, this only works if I create that view programmatically and add it as a subview to my main view. When that view is set up in InterfaceBuilder and defined as an IBOutlet UIImageView this does NOT work. No shadow appears.
So what am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在您的项目中添加一个名为 UIView.swift 的文件(或将其粘贴到任何文件中):
然后,这将在 Interface Builder 中为 Utilities Panel > 中的每个视图提供。属性检查器:
< /a>
您现在可以轻松设置阴影。
备注:
- 阴影只会在运行时出现。
-
clipsToBounds
应该为 false(默认情况下为 false)Add a file named UIView.swift in your project (or just paste this in any file) :
Then this will be available in Interface Builder for every view in the Utilities Panel > Attributes Inspector :
You can easily set the shadow now.
Notes:
- The shadow will only appear at runtime.
-
clipsToBounds
should be false (by default it is)我知道这个问题已经很久了,但最近我遇到了类似的情况,所以我决定为那些处于这种情况的人提供我的答案。
我希望能够通过 Interface Builder 在
UIView
上设置borderColor
和shadowColor
,但是图层的borderColor 的类型
属性是CGColor
(就像shadowColor
),它不是用户定义的运行时属性功能中允许更改的类型之一。因此,我为 CALayer 做了扩展,并添加了两个名为 borderColorIB 和 ShadowColorIB 的 UIColor 类型属性:
RuntimeAttributes.h
RuntimeAttributes.m
现在我已经能够通过 Interface Builder 设置这两个属性,如下所示:
确保选择了 UIView,并添加以下运行时属性:
<- 我用于设置 borderColor 的自定义属性
<- 我用于设置shadowColor的自定义属性
这里有一张图片向您展示我是如何做的:
... 结果将是在运行时明显,而不是在 Xcode 中:
我希望这可以帮助一些人!
I know this question has long, but recently i was in a similar situation so i decided to put my answer for those who are in a situation like this.
I wanted to be able to set the
borderColor
andshadowColor
on anUIView
through the Interface Builder, but the type of a layer’sborderColor
property isCGColor
(just likeshadowColor
) which is not one of the types allowed to be changed in the user-defined runtime attributes feature.So i made an extension for
CALayer
and i added two properties called borderColorIB and shadowColorIB that are of type UIColor:RuntimeAttributes.h
RuntimeAttributes.m
Now i alredy be able to set this two properties through Interface Builder like this:
Make sure the UIView is selected, and add the following runtime attributes:
<- my custom property to set the borderColor
<- my custom property to set the shadowColor
Here is an image to show you how i did:
... and The result will be apparent during runtime, not in Xcode:
i hope this can help some people out there!
我不确定问题是什么 - 确保您的
UIImageView
的clipsToBounds
属性设置为NO
。从 nib 文件加载后,您可以通过引用 IBOutlet 在viewDidLoad
中执行此操作。您不需要将其包装在另一个视图中。编辑
鉴于您需要使用宽高比填充来缩放图像,您可以使用
UIImageView
底层的contentsRect
属性来“模拟”内容剪辑的效果。contentsRect
是图层内容(在本例中为图像)的单位坐标空间中的矩形,它定义了应绘制的内容的子矩形。通过一点数学知识,我们可以通过比较图像视图大小与图像大小(考虑宽高比填充缩放)来找到这个矩形:
这样做,您可以将
clipsToBounds
设置为对于您的图像视图,NO
,但图像仍然会被剪切。如果您需要更改图像视图大小,则将此代码包装到采用UIImageView
作为参数的方法中可能会很方便。我希望这有帮助。
I'm not sure what the issue is - ensure your
UIImageView
'sclipsToBounds
property is set toNO
. You can do this inviewDidLoad
after loading from the nib file by referencing your IBOutlet. You shouldn't need to wrap it in another view.Edit
In light of you needing your image to be scaled using aspect fill, you can use the
contentsRect
property of theUIImageView
's underlying layer to 'simulate' the effect of the contents clipping.contentsRect
is rectangle in the unit coordinate space of the layer's content (in this case your image) that defines a sub-rectangle of the contents that should be drawn.With a little bit of maths, we can find this rectangle by comparing the image view size with the image size (accounting for the aspect fill scaling):
Doing this, you can leave
clipsToBounds
set toNO
for your image view, but the image will still appear clipped. If you need to change the image view size at all, it might be convenient to wrap this code up into a method that takes aUIImageView
as a parameter.I hope this helps.
mauricioconde 的答案的 Swift 5 Xcode 11.3.1 版本。请注意,如果没有@IBInspectable,它将无法工作。
Swift 5 Xcode 11.3.1 version of mauricioconde's answer. Note that without @IBInspectable it wouldn't work.
带圆角半径的 UIView 阴影 + 界面生成器 - swift4
UIView shadow with corner radius + interface builder - swift4