add Shadow on UIView using swift 3

后端 未结 18 1272
攒了一身酷
攒了一身酷 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:08

    Although the accepted answer is great and it works as it should, I've modified it to split offSet: CGSize to offsetX: CGFloat and offsetY: CGFloat.

    extension UIView {
      func dropShadow(offsetX: CGFloat, offsetY: CGFloat, color: UIColor, opacity: Float, radius: CGFloat, scale: Bool = true) {
        layer.masksToBounds = false
        layer.shadowOffset = CGSize(width: offsetX, height: offsetY)
        layer.shadowColor = color.cgColor
        layer.shadowOpacity = opacity
        layer.shadowRadius = radius
        layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
        layer.shouldRasterize = true
        layer.rasterizationScale = scale ? UIScreen.main.scale : 1
      }
    }
    

提交回复
热议问题