[英]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.