vishnu
vishnu

Reputation: 233

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.

Upvotes: 2

Views: 1018

Answers (1)

Stefan
Stefan

Reputation: 936

enter image description hereAs 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

Upvotes: 0

Related Questions