0
votes

I want to apply some round corners to the bottom and top left of a view inside a table cell. And I made this extension on UIView:

extension UIView {
    func round(_ corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}

But I am not sure where exactly to call this on my view to display it correctly. Right now I have it called in layoutSubviews() of my custom cell, but the bottom left part is not round when I start my app, only after the cell goes out of the screen and gets redrawn.

What should be a good place to set the round corners inside a UITableViewCell ?

edit:

Here is the screenshot with the hierarchy: enter image description here

Code:

override func layoutSubviews() {
    super.layoutSubviews()

    configureView()
}

func configureView() {
    bgView.layer.cornerRadius = 3.0
    period.round([.bottomLeft, .topLeft], radius: 3.0)
    bgView.layer.masksToBounds = false
    period.layer.masksToBounds = false
    bgView.layer.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor
    bgView.layer.shadowOffset = CGSize(width: 0, height: 0)
    bgView.layer.shadowOpacity = 0.8
}

edit 2:

enter image description here

1
I would simply set the cornerRadius of the layer itself, rather than using a CAShapeLayer. - fishinear
How would I select the corners which I want to be round ? - Adrian
Ah, Ok, I assumed you wanted all corners to be round. - fishinear
The following question might be related: stackoverflow.com/questions/36241103/… - fishinear
Yeah, I saw that too, I tried increasing it, but still nothing changes. - Adrian

1 Answers

1
votes

This is my CustomTableViewCell class

import UIKit

class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var period: UIView!
    @IBOutlet weak var bgView: UIView!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        bgView.layer.cornerRadius = 5.0

        // you might not be doing this earlier
        bgView.clipsToBounds = true

        period.round([.bottomLeft,.topLeft], radius: 5.0)

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

My TableView datasource methods

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
    return cell
}

enter image description here

My view hierarchy

enter image description here