UITableViewCell圆角问题

时间:2018-05-23 11:34:58

标签: ios iphone swift sdk

enter image description here

我想将最后UITableViewCell个底角设为圆形,我能够做到,但结果是边界没有正确绘制,任何人都有这种类型的问题,请帮助我。谢谢

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

3 个答案:

答案 0 :(得分:1)

使用UIBezierPath将拐角舍入到最后一个单元格

    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
    }

答案 1 :(得分:0)

然后你可以将你的桌面视图轮回到角落。

答案 2 :(得分:0)

使用contentView并在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
    }

}

你的细胞

  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()
        }