Search code examples
swiftuiviewshadowdropshadow

UIView shadow not showing in iphone 5s


I have a UIView and want to add shadow to it. So I get a code given below, which is working good in all iphone simulators and devices, but doesn't show shadow in iphone 5s and simulator as well.

class ShadowView: UIView {
    override var bounds: CGRect {
        didSet {
            setupShadow()
        }
    }

    private func setupShadow() {
        self.layer.cornerRadius = 2
        self.layer.shadowOffset = CGSize(width: 1, height: 1)
        self.layer.shadowRadius = 4
        self.layer.shadowOpacity = 0.5
        self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 2, height: 2)).cgPath
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = UIScreen.main.scale
    }
}

Solution

  • I hope this helps you!!

    import UIKit
    
    class ShadowView: UIView {
    
        override func awakeFromNib() {
            super.awakeFromNib()
    
            setupView()
        }
    
        func setupView() {
            self.layer.cornerRadius = 5.0
            self.layer.shadowOpacity = 1.0
            self.layer.shadowColor = UIColor.black.cgColor
            self.layer.shadowOffset = CGSize(width: -1, height: 1)
            self.layer.masksToBounds = false
            self.layer.shadowRadius = 3.0
            self.layer.shouldRasterize = true
    
        }
    
    }