Sujit Baranwal
Sujit Baranwal

Reputation: 91

UITableViewCell rounded corner issue

enter image description here

I want to make the last UITableViewCell bottom corners circular, I am able to do it, but it results that the boundary is not properly drawed, anyone have same experience with such type of issue, please help me. thanks

cell.roundCorners([.bottomLeft, .bottomRight], radius: 8)
cell.clipsToBounds = true
cell.layer.masksToBounds = true
cell.layoutSubviews()

Upvotes: 1

Views: 389

Answers (3)

Abdelahad Darwish
Abdelahad Darwish

Reputation: 6067

use contentView and add your code at willDisplay

extension UIView {

    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
    }

}

Your Cell

  override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
            cell.contentView.roundCorners([.bottomLeft, .bottomRight], radius: 8)
            cell.contentView.clipsToBounds = true
            cell.contentView.layer.masksToBounds = true
            cell.contentView.layoutSubviews()
        }

Upvotes: 0

Mitesh Dobareeya
Mitesh Dobareeya

Reputation: 1020

Use UIBezierPath for rounding corner to last cell

    let totalRow : Int = tableView.numberOfRows(inSection: indexPath.section)
    //first get total rows in that section by current indexPath.
    if indexPath.row == totalRow - 1 {
        //this is the last row in section.

        let maskLayer = CAShapeLayer()
        maskLayer.path = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.bottomLeft,.bottomRight], cornerRadii: CGSize(width: 10, height: 10)).cgPath
        cell.layer.mask = maskLayer
    }else{

        let maskLayer = CAShapeLayer()
        maskLayer.path = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [], cornerRadii: CGSize(width: 0, height: 0)).cgPath
        cell.layer.mask = maskLayer
    }

Upvotes: 1

Abhay Singh
Abhay Singh

Reputation: 265

then instead of cell you can make ur tableview round cornered.

Upvotes: 0

Related Questions