I need to round the bottom left and right corners of a view and base on what I found on the internet I created this function inside an UIview extension:
func setRoundedCorner(withRadius radius: CGSize, forCorners corners: UIRectCorner ) {
let viewShape = CAShapeLayer()
viewShape.bounds = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.bounds.width, height: self.bounds.height)
viewShape.position = self.center
viewShape.path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: radius).cgPath
self.layer.mask = viewShape
}
Now, I encountered two issues with this solution; despite it works perfectly for a static view, my view changes its height programmatically causing the view to be truncated; I found a workaround to that issue recalling each time the setRoundedCorner
method, but it's very uncomfortable inside my view; is there any other way to solve this? Another issue harder to me to solve is the shadow set. I usually use this method:
func setShadow(height: CGFloat, width: CGFloat, opacity: Float, radius: CGFloat) {
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.layer.cornerRadius).cgPath
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOpacity = opacity
self.layer.shadowOffset = CGSize(width: width, height: height)
self.layer.cornerRadius = radius
self.layer.masksToBounds = false
}
Instead the view appears this way when it should has the shadow under itself:
But in this situation doesn't seem to work not showing any shadow at all. Can anyone give me some advice?