iOS add / remove shadow from a view

后端 未结 6 2190
遇见更好的自我
遇见更好的自我 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:56

    I tried the other answers, the only thing that worked for me was to toggle layer.masksToBounds to true/false

    lazy var myView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .white
        view.layer.cornerRadius = 5
        view.layer.shadowColor = UIColor.black.cgColor
        view.layer.shadowOpacity = 3
        view.layer.shadowOffset = .zero
        view.layer.shadowRadius = 5
        return view
    }()
    
    func showShadow() {
    
        myView.layer.masksToBounds = false
    }
    
    
    func hideShadow() {
    
        myView.layer.masksToBounds = true
    }
    

提交回复
热议问题