简体   繁体   中英

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

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.

在此处输入图片说明 As of iOS11, CALayer has property 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.

Also you need to make sure that the contentView is clipping its subviews so that they can conform to the shape

cell.contentView.clipsToBounds = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM