I built a view with only the two upper corners that have a corner radius and the two bottom ones that are normal 90deg angles:
extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
I add this to my view by simply doing:
override func layoutSubviews() {
super.layoutSubviews()
roundCorners(corners: [.topLeft, .topRight], radius: 15)
}
Now I want to add a shadow to it, and I tried doing the following:
private var shadowLayer: CAShapeLayer!
override func layoutSubviews() {
super.layoutSubviews()
let corners: UIRectCorner = [.topLeft, .topRight]
let radius: CGFloat = 15
roundCorners(corners: corners, radius: radius)
if shadowLayer == nil {
shadowLayer = CAShapeLayer()
shadowLayer.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
shadowLayer.fillColor = UIColor.white.cgColor
shadowLayer.shadowColor = UIColor.darkGray.cgColor
shadowLayer.shadowPath = shadowLayer.path
shadowLayer.shadowOffset = CGSize(width: 2.0, height: 2.0)
shadowLayer.shadowOpacity = 0.3
shadowLayer.shadowRadius = 2
layer.insertSublayer(shadowLayer, at: 0)
}
}
But this doesn't appear to work (the shadow is not appearing at all) How can I fix this?
Thank you