首页 > 解决方案 > 如何使用圆角向 UIView 添加内部阴影

问题描述

UIView我必须为圆角添加一个内部阴影。

我在 SO 上看到了几个答案,它们向 UIViews 添加了内部阴影,但它们不能按我的意愿工作,因为它们将阴影添加到整个矩形,而不是考虑圆角。

将此图像视为我想要实现的示例:

在此处输入图像描述

标签: iosswiftxcodeuiview

解决方案


这是一个把戏。您将阴影和边框赋予同一视图,阴影将落在视图内。请确保视图的背景颜色清晰。使用下面的代码作为参考。

yourView.layer.shadowColor = UIColor.gray.cgColor
yourView.layer.shadowOpacity = 0.3
yourView.layer.shadowOffset = CGSize.zero
yourView.layer.shadowRadius = 6
yourView.layer.masksToBounds = true
yourView.layer.borderWidth = 1.5
yourView.layer.borderColor = UIColor.white.cgColor
yourView.layer.cornerRadius = imageView.bounds.width / 2

推荐阅读