add Shadow on UIView using swift 3

后端 未结 18 1187
攒了一身酷
攒了一身酷 2020-11-30 19:49

prior swift 3 i was adding shadow in my UIView like this :

//toolbar is an UIToolbar (UIView)
toolbar.layer.masksToBounds = false
toolbar.layer.shadowOffset          


        
相关标签:
18条回答
  • 2020-11-30 20:13

    Please try this, it's working for me.

    extension UIView {
    
    
    func dropShadow() {
    
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOffset = CGSize(width: 2, height: 3)
        layer.masksToBounds = false
    
        layer.shadowOpacity = 0.3
        layer.shadowRadius = 3
        //layer.shadowPath = UIBezierPath(rect: bounds).cgPath
        layer.rasterizationScale = UIScreen.main.scale
        layer.shouldRasterize = true
    }}
    
    0 讨论(0)
  • 2020-11-30 20:18

    After spent lot of hours, I just find out the solution, just add this simple line.

    backgroundColor = .white

    Hope this help you!

    0 讨论(0)
  • 2020-11-30 20:19

    If you need rounded shadow. Works for swift 4.2

    extension UIView {
    
            func dropShadow() {
    
                var shadowLayer: CAShapeLayer!
                let cornerRadius: CGFloat = 16.0
                let fillColor: UIColor = .white
    
                if shadowLayer == nil {
                    shadowLayer = CAShapeLayer()
    
                    shadowLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath
                    shadowLayer.fillColor = fillColor.cgColor
    
                    shadowLayer.shadowColor = UIColor.black.cgColor
                    shadowLayer.shadowPath = shadowLayer.path
                    shadowLayer.shadowOffset = CGSize(width: -2.0, height: 2.0)
                    shadowLayer.shadowOpacity = 0.8
                    shadowLayer.shadowRadius = 2
    
                    layer.insertSublayer(shadowLayer, at: 0)
                }
            }
        }
    

    Swift 4 rounded UIView with shadow

    0 讨论(0)
  • 2020-11-30 20:23

    Swift 5 Just call this function and pass your view

    public func setViewSettingWithBgShade(view: UIView)
    {
        view.layer.cornerRadius = 8
        view.layer.borderWidth = 1
        view.layer.borderColor = AppTextFieldBorderColor.cgColor
    
        //MARK:- Shade a view
        view.layer.shadowOpacity = 0.5
        view.layer.shadowOffset = CGSize(width: 1.0, height: 1.0)
        view.layer.shadowRadius = 3.0
        view.layer.shadowColor = UIColor.black.cgColor
        view.layer.masksToBounds = false
    }
    
    0 讨论(0)
  • 2020-11-30 20:25

    Please Try this

    func applyShadowOnView(_ view: UIView) {
        view.layer.cornerRadius = 8
        view.layer.shadowColor = UIColor.darkGray.cgColor
        view.layer.shadowOpacity = 1
        view.layer.shadowOffset = .zero
        view.layer.shadowRadius = 5
    }
    
    0 讨论(0)
  • 2020-11-30 20:26

    This works for me (Swift 3 and 4)

    yourView.layer.shadowColor = UIColor.gray.cgColor
    yourView.layer.shadowOpacity = 0.3
    yourView.layer.shadowOffset = CGSize.zero
    yourView.layer.shadowRadius = 6
    
    0 讨论(0)
提交回复
热议问题