【问题标题】:how to set corner radius for top left and top right of a UIView on tableview Custom Cell?如何在tableview Custom Cell上为UIView的左上角和右上角设置角半径?
【发布时间】:2018-10-12 16:52:49
【问题描述】:

ViewController.swift

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "mycell") as! CustomTableViewCell

    cell.backgroundColor = UIColor.clear

    cell.nameLbl.text = "\(adminNmaes[indexPath.row])"
    cell.aboutLbl.text = "\(aboutarray[indexPath.row])"
    if indexPath.row == 0
    {
        cell.view1.roundCorners(corners: [.topLeft,.topRight], radius: 10)
       // tableView.reloadData()
    }

    return cell
}

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

我使用自动布局设置自定义单元格类,并使用扩展程序仅对第一个单元格应用角半径。我还想为页脚的左下角和右下角提供圆角半径,并将其填充为 10。感谢任何帮助。提前致谢。

【问题讨论】:

    标签: ios swift xcode uitableview


    【解决方案1】:

    iOS11 开始,CALayer 有属性 maskedCorners:

    var maskedCorners: CACornerMask { get set }

    在你的情况下,你会使用:

    cell.contentView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMinYCorner]

    这会将左上角和右上角遮盖到您设置的任何cornerRadius。

    您还需要确保 contentView 正在裁剪其子视图,以便它们符合形状

    cell.contentView.clipsToBounds = true

    【讨论】:

    • 你能解释一下吗?我的问题是,当我在单元格中为行索引路径设置角半径时,第一个单元格消失了,如果我尝试将其滚动到顶部,那么只有它才会出现
    • @vishnu 你的 CustomTableViewCell 类里面有什么图片?也尝试更新的答案
    • 我会发布一张图片,我有一个 uiview,上面有一个按钮和 2 个标签
    • @vishnu 将您的编辑添加到您提出的问题而不是答案。此外,您正在将您的 backgroundColor 设置为在问题中清除。也尝试更新的答案
    • 我将此行添加到我的单元格中的 indexpath 函数“cell.view1.layer.maskedCorners = [.layerMinXMinYCorner] cell.view1.clipsToBounds = true”处的行仍然没有变化
    猜你喜欢
    • 2020-05-07
    • 2012-04-27
    • 2014-10-26
    • 2020-10-27
    • 2019-07-29
    • 1970-01-01
    • 2012-02-17
    • 2015-09-22
    相关资源
    最近更新 更多