I have this UITableViewCell
class MyCell: UITableViewCell {
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var firstPartView: UIView!
override func awakeFromNib() {
super.awakeFromNib()
//container view corner radius
self.containerView.layer.cornerRadius = 10.0
self.containerView.layer.masksToBounds = true
self.containerView.layer.isOpaque = false
//shadow
self.containerView.layer.shadowColor = UIColor.black.cgColor
self.containerView.layer.shadowOffset = CGSize(width: 0, height: 1.0)
self.containerView.layer.shadowOpacity = 0.2
self.containerView.layer.shadowRadius = 4.0
//gradient color
self.firstPartView.setGradientBackground(startColor: UIColor(red: 0.224, green: 0.592, blue: 1.0, alpha: 1.0), endColor: UIColor(red: 0.553, green: 0.765, blue: 1.0, alpha: 1.0))
//rounded corners (only topLeft and bottomLeft)
self.firstPartView.roundCorners(corners: [.topLeft, .bottomLeft], radius: 10.0)
}
}
Where roundCorners
function is:
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
FirstPartView is a simple view on the left of container view, like so:
But this is the final result:
As you can see, the firstPartView has incorrect height.
Reading about this error I found that in awakeFromNib
function the rects are not drawn yet so I tried removing the roundCorners
call from awakeFromNib
and I created a new UIView class like this:
class RoundedCornerFix: UIView {
override func layoutSubviews() {
super.layoutSubviews()
self.roundCorners(corners: [.topLeft, .bottomLeft], radius: 10.0)
}
}
Unfortunately with no luck. This is the result:
I also did this change:
override func awakeFromNib() {
super.awakeFromNib()
[...]
//rounded corners (only topLeft and bottomLeft)
self.firstPartView.layoutIfNeeded()
self.firstPartView.roundCorners(corners: [.topLeft, .bottomLeft], radius: 10.0)
}
But the problem is the same