iOS add / remove shadow from a view

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

    Swift 5.+

    My solution was to add a shadowBackgroundView, which has a removable shadowLayer. In this way I could easyly remove the layer without resetting the shadow properties.

    class ViewController: UIViewController {
    
        private let shadowBackgroundView: UIView = {
            let view = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
            view.layer.masksToBounds = false
            return view
        }()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
    
            view.addSubview(shadowBackgroundView)
    
            // the view you want to add the shadow
            let dummyView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
            dummyView.backgroundColor = .red
            shadowBackgroundView.addSubview(dummyView)
    
            let addShadowButton = UIButton(frame: CGRect(x: 100, y: 300, width: 140, height: 50))
            addShadowButton.backgroundColor = .blue
            addShadowButton.setTitle("Add Shadow", for: .normal)
            addShadowButton.addTarget(self, action: #selector(addShadow), for: .touchUpInside)
    
            let removeShadowButton = UIButton(frame: CGRect(x: 100, y: 450, width: 140, height: 50))
            removeShadowButton.backgroundColor = .blue
            removeShadowButton.setTitle("Remove Shadow", for: .normal)
            removeShadowButton.addTarget(self, action: #selector(removeShadow), for: .touchUpInside)
    
            view.addSubview(addShadowButton)
            view.addSubview(removeShadowButton)
        }
    
        @objc
        func addShadow() {
            let shadowLayer = CALayer()
            shadowLayer.name = "ShadowLayer"
            shadowLayer.shadowColor = UIColor.black.cgColor
            shadowLayer.shadowOpacity = 1
            shadowLayer.shadowOffset = .zero
            shadowLayer.shadowRadius = 10
            shadowLayer.shadowPath = UIBezierPath(rect: shadowBackgroundView.bounds).cgPath
            // Otherwise the shadow will appear above the dummyView
            shadowLayer.zPosition = -1
            shadowBackgroundView.layer.addSublayer(shadowLayer)
        }
    
        @objc
        func removeShadow() {
            // Alternatively, you could also create the shadowLayer as a property, so you could call shadowLayer.removeFromSuperLayer()
            shadowBackgroundView.layer.sublayers?.first { $0.name == "ShadowLayer" }?.removeFromSuperlayer()
        }
    }
    

    As a Note, for the UITableViewCell, you wouldnt need to add a shadowBackgroundView, but you could add the shadowLayer directly to cell.view.layer, which serves as the backgroundView for the cell.contentView.

提交回复
热议问题