0

I have this UITableViewCell

class MyCell: UITableViewCell {

    @IBOutlet weak var containerView: UIView!

    @IBOutlet weak var firstPartView: UIView!

    override func awakeFromNib() {
         super.awakeFromNib()

         //container view corner radius
         self.containerView.layer.cornerRadius = 10.0
         self.containerView.layer.masksToBounds = true
         self.containerView.layer.isOpaque = false

         //shadow
         self.containerView.layer.shadowColor = UIColor.black.cgColor
         self.containerView.layer.shadowOffset = CGSize(width: 0, height: 1.0)
         self.containerView.layer.shadowOpacity = 0.2
         self.containerView.layer.shadowRadius = 4.0

         //gradient color
         self.firstPartView.setGradientBackground(startColor: UIColor(red: 0.224, green: 0.592, blue: 1.0, alpha: 1.0), endColor: UIColor(red: 0.553, green: 0.765, blue: 1.0, alpha: 1.0))

         //rounded corners (only topLeft and bottomLeft)
         self.firstPartView.roundCorners(corners: [.topLeft, .bottomLeft], radius: 10.0)    
     }
}

Where roundCorners function is:

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
}

FirstPartView is a simple view on the left of container view, like so:

AutoLayout

But this is the final result:

enter image description here

As you can see, the firstPartView has incorrect height. Reading about this error I found that in awakeFromNib function the rects are not drawn yet so I tried removing the roundCorners call from awakeFromNib and I created a new UIView class like this:

class RoundedCornerFix: UIView {
    override func layoutSubviews() {
        super.layoutSubviews()
        self.roundCorners(corners: [.topLeft, .bottomLeft], radius: 10.0)
    }     
 }

Unfortunately with no luck. This is the result:

enter image description here

I also did this change:

override func awakeFromNib() {
    super.awakeFromNib()

    [...]

    //rounded corners (only topLeft and bottomLeft)
    self.firstPartView.layoutIfNeeded()
    self.firstPartView.roundCorners(corners: [.topLeft, .bottomLeft], radius: 10.0)    
}

But the problem is the same

Ernesto Schiavo
  • 1,020
  • 2
  • 12
  • 33
  • can you share what's your expectation? if height is the only issue, check your constraints, or code that create cell height if any – Satish Oct 03 '18 at 20:42
  • And can you show the code of your `roundCorners` function? – André Slotta Oct 03 '18 at 20:45
  • the roundCorners function is inside the question. I want the height of the blue view like the container view but the autolayout constraint are correct! I think the problem is something related to what this user says (https://stackoverflow.com/a/41197790/4734139) but I can not resolve – Ernesto Schiavo Oct 03 '18 at 20:55
  • You create your mask layer when the cell is first created, but I don't see any code that suggests you are recreating it when the cell is resized. Could the table view be resizing the cell (and your `firstPartView` because of the constraints) after the mask layer has been created and put in place? – Scott Thompson Oct 03 '18 at 22:45

0 Answers0