What's the best way to add a drop shadow to my UIView

前端 未结 7 1167
自闭症患者
自闭症患者 2020-12-04 05:20

I am trying to add a drop shadow to views that are layered on top of one another, the views collapse allowing content in other views to be seen, in this vein i want to keep

7条回答
  •  独厮守ぢ
    2020-12-04 05:55

    On viewWillLayoutSubviews:

    override func viewWillLayoutSubviews() {
        sampleView.layer.masksToBounds =  false
        sampleView.layer.shadowColor = UIColor.darkGrayColor().CGColor;
        sampleView.layer.shadowOffset = CGSizeMake(2.0, 2.0)
        sampleView.layer.shadowOpacity = 1.0
    }
    

    Using Extension of UIView:

    extension UIView {
    
        func addDropShadowToView(targetView:UIView? ){
            targetView!.layer.masksToBounds =  false
            targetView!.layer.shadowColor = UIColor.darkGrayColor().CGColor;
            targetView!.layer.shadowOffset = CGSizeMake(2.0, 2.0)
            targetView!.layer.shadowOpacity = 1.0
        }
    }
    

    Usage:

    sampleView.addDropShadowToView(sampleView)
    

提交回复
热议问题