简体   繁体   English

如何在TableView自定义单元格上为UIView的左上角和右上角设置拐角半径?

[英]how to set corner radius for top left and top right of a UIView on tableview Custom Cell?

ViewController.swift 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
    }
}

i setup custom cell class using autolayout and used an extension to apply cornerradius for only 1st cell. 我使用自动布局设置自定义单元格类,并使用扩展名仅对第一个单元格应用角半径。 i aslo want to give corner radius for bottom left and bottom right for the footer and also give it a padding of 10. any help is appreciated.thanks in advance. 我也希望为页脚的左下角和右下角提供圆角半径,并给其填充10。如有任何帮助,不胜感激。

在此处输入图片说明 As of iOS11, CALayer has property maskedCorners: 从iOS11开始,CALayer具有属性maskedCorners:

var maskedCorners: CACornerMask { get set }

In your case you would use: 在您的情况下,您将使用:

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

This masks the top left and right corners to whatever cornerRadius you've set. 这会将左上角和右上角遮罩到您设置的任何cornerRadius。

Also you need to make sure that the contentView is clipping its subviews so that they can conform to the shape 另外,您还需要确保contentView裁剪其子视图,以便它们可以符合形状

cell.contentView.clipsToBounds = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 UIView 中为左上角和右上角的圆角半径仅设置顶部、左侧和右侧的边框? - how to set boarder for only top, left and right with corner radius with top-left and top-right in UIView? 如何在Iphone +上仅在表格视图自定义单元的左上角和右上角半径 - How to corner radius only top left and top right of table view custom cell on Iphone + 自定义表格视图的左上角和右上角 header - Custom top left and top right corner of a tableview header 如何仅为 UIView 的左上角和右上角设置cornerRadius? - How to set cornerRadius for only top-left and top-right corner of a UIView? 如何为 UIView 的左下角、右下角和左上角设置 cornerRadius? - how to set cornerRadius for only bottom-left,bottom-right and top-left corner of a UIView? 右上角和左上角半径无法正常工作 - Top right and top left corner radius is not working properly 为什么自定义视图的角半径仅应用于左上角和右上角? - Why is corner radius applied only to top left and right corner for a custom view? 将文本对齐到TableView中单元格左上角的简单方法? - Simple way of aligning text to the top left corner of a cell in a TableView? 如何为自定义单元格设置角半径 - How to set a corner radius for a custom cell 如何在TableView单元格的视图内部设置视图的角半径? - How to set corner radius of a view inside a view in tableview cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM