Reputation: 3
I want to round top and bottom corners of 2 UITableViewCell like this : It's what I get on iOS 11
But I got this on iOS 10:
Result rectShape.bounds = self.bounds
With that code :
UITableViewCell of blue view
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 of purple view
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
}
}
}
if I replace self.bounds by the CGRect in comment I got that :
I try to change the self.bounds at the beginning of init() but it's doesn't work too
And I also try to put the code which change corners in layoutSubviews() but in that case the UITableViewCell disappear
I don't one to use only one UITableViewCell and round his corners I really need to round bottom and top corners on to separate UITableViewCell
If you know how I can fix that I will be very grateful Thank you for your time
Upvotes: 0
Views: 1412
Reputation: 2415
Well, i am using this very simple extension of UIView to make corners round. In cellForRowAt delegate method, after dequeing the cell, just call
cell.makeCornersRound()
extension UIView {
public func makeCornersRound () {
self.layer.cornerRadius = 8
self.clipsToBounds = true
}
}
Upvotes: 0
Reputation: 452
Add the following extension:
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
}
Then call it from layout subviews like below:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
headerView.roundCorners([UIRectCorner.topLeft, UIRectCorner.topRight], radius: kTableViewCornerRadius)
}
Upvotes: 1