Закругленный UITableViewCell только нижний или верхний углы, но это не работает Swift iOS 10 - PullRequest
0 голосов
/ 06 июня 2018

Я хочу скруглить верхние и нижние углы 2 UITableView. Ячейка выглядит так: Это то, что я получаю на iOS 11

Что я хочу

Но я получил это наiOS 10:

Результат 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
    }
}
}

если я заменил self.bounds на CGRect в комментарии, я получил следующее:

С rectShape.bounds = CGRect (x: self.bounds.origin.x + 16, y: self.bounds.origin.y, ширина: self.bounds.size.width - 16, высота: self.bounds.size.height)

Iпопробуйте изменить self.bounds в начале init (), но он тоже не работает

И я также пытаюсь поместить код, изменяющий углы, в layoutSubviews (), но в этом случае UITableViewCell исчезает

Мне не нужно использовать только один UITableViewCell и закруглять его углы, мне действительно нужно округлить нижний и верхний углы, чтобы отделить UITableViewCell

Если вы знаете, как я могу это исправить, я буду очень благодарен Спасибо за ваше время

Ответы [ 2 ]

0 голосов
/ 07 июня 2018

Ну, я использую это очень простое расширение UIView, чтобы закруглить углы.В методе делегата cellForRowAt после удаления ячейки просто вызовите

cell.makeCornersRound()

extension UIView {

  public func makeCornersRound () {

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

  }

}
0 голосов
/ 06 июня 2018

Добавьте следующее расширение:

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)
}
...