Kai Kuhlmann
Kai Kuhlmann

Reputation: 182

UITableViewCell TopLeft and BottomLeft Corners not working

I'm new to iOS development and trying to create a custom UITableViewCell that has a background which top-left and bottom-left corners I want to round, but it only works for the top-left corner.

Here is my code (Swift 3):

override func awakeFromNib() {
    super.awakeFromNib()

    let bgViewMask = CAShapeLayer()

    let bgViewPath = UIBezierPath(
        roundedRect: bgView.bounds,
        byRoundingCorners: [.topLeft, .bottomLeft],
        cornerRadii: CGSize(width: 8, height: 8)
    )

    bgViewMask.frame = bgView.bounds
    bgViewMask.path = bgViewPath.cgPath

    bgView.layer.mask = bgViewMask
}

I also tried this in layoutSubviews()

override func layoutSubviews() {
    super.layoutSubviews()

    //code here
}

but it didn't work as well.

If I use bgView.layer.cornerRadius = 8 it works for all corners.

Am I doing anything wrong?

Upvotes: 0

Views: 527

Answers (1)

Son Pham
Son Pham

Reputation: 1634

You can try this:

import UIKit
class CustomImageView: UIImageView {

override public func layoutSubviews() {
    super.layoutSubviews()
    self.roundUpCorners([.topLeft, .bottomLeft], radius: 8)
}
}
extension UIView {
func roundUpCorners(_ 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
}
}

Upvotes: 3

Related Questions