ios - 在 swift4 中向 UIView 添加云阴影

标签 ios swift uiview

I want to add shadow(it is not like default it somehow clouded and blurred) for my UIView like below

enter image description here

我写了一个扩展

func addShadow(color: UIColor = UIColor.black, opacity: Float = 0.9, radius: CGFloat = 1, scale: Bool = true) {
        self.layer.masksToBounds = false
        self.layer.shadowColor = color.cgColor

        self.layer.shadowOpacity = opacity
        self.layer.shadowRadius = radius

        self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = scale ? UIScreen.main.scale : 1
    }

输出: enter image description here

但无法获得准确的输出。 您的帮助将不胜感激。

最佳答案

我相信您还需要设置 clipsToBounds

self.clipsToBounds = false

关于ios - 在 swift4 中向 UIView 添加云阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47438426/

相关文章:

ios - View 更改后下推 TableView

ios - 为什么我不能在 Realm 中保存数据

ios - 通过标签动态查找 iOS UIView

ios - 如何添加模糊背景和弹出 View ?

ios - CoreData保存对象不成功

ios - 填充 UISearchBarController 结果 TableView 时遇到问题

ios - 无法让我的位置显示在 iOS 8 的 Google map 中

swift - 安全区域布局指南不适用于 UITableView 的 BackgroundView

ios - preferredInterfaceOrientationForPresentation 不工作

iphone - UIViewController 中 subview 的布局和大小