ios - 如何在UIView顶部设置阴影?

标签 ios swift

class AccountListViewController: UIViewController {       

    @IBOutlet weak var actionButtonView: UIView!

    override func viewDidLoad() {            
       super.viewDidLoad()
       actionButtonView.layer.cornerRadius = 10
       actionButtonView.layer.shadowColor = UIColor.gray.cgColor
       actionButtonView.layer.shadowOffset = .zero
       actionButtonView.layer.shadowOpacity = 0.6
       actionButtonView.layer.shadowRadius = 10
    }

我已经尝试过,但没有得到结果

最佳答案

您的代码很好,但您需要为阴影偏移指定负值而不是 .zero

class AccountListViewController: UIViewController {

    @IBOutlet weak var actionButtonView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        actionButtonView.layer.cornerRadius = 10
        actionButtonView.layer.shadowColor = UIColor.gray.cgColor
        actionButtonView.layer.shadowOffset = CGSize(width: 0.0, height : -5.0)
        actionButtonView.layer.shadowOpacity = 0.6
        actionButtonView.layer.shadowRadius = 10
    }
}

为了更方便,您可以为此创建一个扩展。

请引用以下代码

extension UIView {
    func addTopShadow(shadowColor : UIColor, shadowOpacity : Float,shadowRadius : Float,offset:CGSize){
        self.layer.shadowColor = shadowColor.cgColor
        selflayer.shadowOffset = offset
        self.layer.shadowOpacity = shadowOpacity
        self.layer.shadowRadius = shadowRadius
        self.clipsToBounds = false
    }
}

如何使用?

viewName.addTopShadow(shadowColor: UIColor.gray, shadowOpacity: 0.9, shadowRadius: 10, offset: CGSize(width: 0.0, height : -5.0))

关于ios - 如何在UIView顶部设置阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56123148/

相关文章:

swift - 什么时候不需要使用 lambda 执行来初始化惰性变量?

ios - 如何在 Swift 3 中用户触摸时从 UITextField 中删除文本?

具有后台获取功能的 iOS 应用程序在后台崩溃,异常 0xbad5bbad

ios - 如何让 Siri 在特定关键字上启动应用程序?

ios - OTA (Over the air) IOS 7.1 更改 http-https 后仍然无法连接服务器?

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

ios - 使用 Simperium for iOS 禁用实体同步

ios - 无法获取 didSelectRowAtIndexPath 以注册点击

json - 快速从解析的 json 中获取字典 var

ios - 使用 ARKit 将屏幕坐标映射到面部网格的纹理坐标