圆形UITableViewCell只有底部或顶部角落,但它不起作用Swift iOS 10

时间:2018-06-06 11:39:13

标签: ios swift uitableview rounded-corners

我想围绕2 UITableViewCell的顶角和底角:这是我在iOS 11上得到的东西

What I want

但我在iOS 10上得到了这个:

Result rectShape.bounds = self.bounds

使用该代码:

蓝色视图的UITableViewCell

import UIKit

class ProductDescriptionTableViewCell: UITableViewCell {

var ProductDescription: UITextView!
public var container: UIView!

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
    // Set the width of the cell
   self.bounds = CGRect(x: self.bounds.origin.y + 16, y: self.bounds.origin.y, width: self.bounds.size.width - 16, height: self.bounds.size.height)

    super.layoutSubviews()
}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    self.backgroundColor = UIColor.blue
    if #available(iOS 11.0, *) {
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 10
        self.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
    } else {
        let rectShape = CAShapeLayer()
        rectShape.bounds = self.bounds// CGRect(x: self.bounds.origin.x + 16, y: self.bounds.origin.y, width: self.bounds.size.width - 16, height: self.bounds.size.height)
        rectShape.position = self.center
        rectShape.path = UIBezierPath(roundedRect:  rectShape.bounds, byRoundingCorners: [.bottomRight, .bottomLeft], cornerRadii: CGSize(width: 10, height: 10)).cgPath

        self.layer.backgroundColor = UIColor.red.cgColor
        //Here I'm masking the textView's layer with rectShape layer
        self.layer.mask = rectShape
    }
}
}

紫色视图的UITableViewCell

import UIKit

class ProductTitleTableViewCell: UITableViewCell {

var ProductTitle: UILabel!

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
    // Set the width of the cell
    self.bounds = CGRect(x: self.bounds.origin.y + 16, y: self.bounds.origin.y, width: self.bounds.size.width - 16, height: self.bounds.size.height)

    super.layoutSubviews()
}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    self.backgroundColor = UIColor.purple

    if #available(iOS 11.0, *) {
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 10
        self.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
    } else {
        let rectShape = CAShapeLayer()
         rectShape.bounds = self.bounds//CGRect(x: self.bounds.origin.x + 16, y: self.bounds.origin.y, width: self.bounds.size.width - 16, height: self.bounds.size.height)
         rectShape.position = self.center
         rectShape.path = UIBezierPath(roundedRect:  rectShape.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 10, height: 10)).cgPath

         self.layer.backgroundColor = UIColor.green.cgColor
         //Here I'm masking the textView's layer with rectShape layer
         self.layer.mask = rectShape
    }
}
}

如果我在评论中用CGRect替换self.bounds我得到了:

With rectShape.bounds = CGRect(x: self.bounds.origin.x + 16, y: self.bounds.origin.y, width: self.bounds.size.width - 16, height: self.bounds.size.height)

我尝试在init()的开头更改self.bounds,但它也不起作用

我还尝试将更改角落的代码放在layoutSubviews()中,但在这种情况下,UITableViewCell会消失

我不是只使用一个UITableViewCell并绕过他的角落我真的需要圆底和顶角来分开UITableViewCell

如果你知道如何解决这个问题,我将非常感激 谢谢你的时间

2 个答案:

答案 0 :(得分:1)

添加以下扩展程序:

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

然后从布局子视图中调用它,如下所示:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    headerView.roundCorners([UIRectCorner.topLeft, UIRectCorner.topRight], radius: kTableViewCornerRadius)
}

答案 1 :(得分:0)

好吧,我正在使用这个非常简单的UIView扩展来制作角落。在cellForRowAt委托方法中,在取消单元格后,只需调用

cell.makeCornersRound()
extension UIView {

  public func makeCornersRound () {

    self.layer.cornerRadius = 8
    self.clipsToBounds = true

  }

}