iOS add / remove shadow from a view

后端 未结 6 2189
遇见更好的自我
遇见更好的自我 2021-02-12 22:14

I do not understand how to remove a shadow that was added to a view. I add to my view in initWithFrame a shadow in this way:

self.layer.borderWidth          


        
6条回答
  •  名媛妹妹
    2021-02-12 22:50

    Swift 4.2

    I am using this in my code for labels and navigation bar.

    extension UIView {
    
        func shadow(_ height: Int = 5) {
            self.layer.masksToBounds = false
            self.layer.shadowRadius = 4
            self.layer.shadowOpacity = 1
            self.layer.shadowColor = UIColor.gray.cgColor
            self.layer.shadowOffset = CGSize(width: 0 , height: height)
        }
    
        func removeShadow() {
            self.layer.shadowOffset = CGSize(width: 0 , height: 0)
            self.layer.shadowColor = UIColor.clear.cgColor
            self.layer.cornerRadius = 0.0
            self.layer.shadowRadius = 0.0
            self.layer.shadowOpacity = 0.0
        }
    }
    

提交回复
热议问题