【问题标题】:UITableViewCell rounded corner issueUITableViewCell 圆角问题
【发布时间】:2018-05-23 11:34:57
【问题描述】:

我想把最后一个UITableViewCell底角做成圆形,我可以做到,但结果是没有正确绘制边界,任何人都对此类问题有相同的经验,请帮助我。谢谢

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

【问题讨论】:

  • 你在哪里调用这个代码?
  • func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { let numberOfRows = tableView.numberOfRows(inSection: indexPath.section) if numberOfRows - 1 == indexPath.row { cell.roundCorners([.bottomLeft, .bottomRight], radius: 8) cell.layoutSubviews() } }
  • 在 willDisplayCell 中调用它,如果您有任何建议,请告诉我
  • 您是否提供了相同的边框颜色和宽度?
  • 如果我要提供边框颜色,我必须设置边框宽度,在这种情况下,单元格的所有四个边框都会被着色并且看起来很奇怪,因为,我正在为 uitableview 使用分隔符

标签: ios iphone swift sdk


【解决方案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
    }

【讨论】:

  • 让 path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners,cornerRadii: CGSize(width: radius, height: radius)) let mask = CAShapeLayer() mask.path = path.cgPath self .layer.mask = mask 我还在用。
【解决方案2】:

然后你可以让你的表格视图变成圆角而不是单元格。

【讨论】:

  • 但是它有多个部分,所以对于tableview它不起作用
  • 所以对于这种情况,您可以将表格页脚视图设为圆角
  • 但我已经在使用分隔符了,如果是页脚视图,它看起来与最后一个单元格是分开的
【解决方案3】:

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

【讨论】:

  • 您需要 tableView 或 tableview 中的最后一个单元格
  • 我需要最后一个单元格是底部圆角,对于 tableview 中的每个部分
  • 这是方法 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 }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-05
  • 2012-11-22
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多