Reputation: 91
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
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
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