prior swift 3 i was adding shadow in my UIView like this :
//toolbar is an UIToolbar (UIView)
toolbar.layer.masksToBounds = false
toolbar.layer.shadowOffset
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
}}
After spent lot of hours, I just find out the solution, just add this simple line.
backgroundColor = .white
Hope this help you!
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
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
}
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
}
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