2

I am trying to make a different corner radius for each corner on a UIView in an expanding tableview cell. I have a UIView with a UILabel as a subview. When there is a lot of text the UILabel expands, expanding the cell. What I am trying to do is make the corner radius of the UIView 13 on the top left and right corners and corner radius of 4 on the bottom corners.

Currently, I am using this question to round all corners with layer.cornerRadius which shows as enter image description here

and then using this code

let maskPath = UIBezierPath(roundedRect: (originalCell?.messageBackground.bounds)!,
                            byRoundingCorners: [.bottomLeft, .bottomRight],
                            cornerRadii: CGSize(width: 13.0, height: 0.0))

let maskLayer = CAShapeLayer()
maskLayer.path = maskPath.cgPath
originalCell?.messageBackground.layer.mask = maskLayer

To try and round the bottom corners of the view, however when I do that the cell shows as this enter image description here

So is there any way to round the top and bottom corners as different radius with expanding cells?

ttorbik
  • 398
  • 5
  • 14

2 Answers2

0

Building on @Glenn's idea I would suggest:

  1. Instead of using heavy image use a light CALayer as a sublayer of your main view, putting it below the UIView's layer.
  2. It should observe the bounds of the view and be redrawn every time the parent view changes its bounds. (For example, 'override var bounds' in the main UIView and catching 'didSet' propagate the new bounds down to the CALayer.
  3. Within the CALayers's draw(_:) method draw a path considering your corners and then fill it with gray color.
rommex
  • 763
  • 1
  • 8
  • 21
0

You can use this line of code:

yourView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMaxYCorner]

It makes corners in specific corners you need:

  1. layerMaxXMaxYCorner : bottom right corner
  2. layerMaxXMinYCorner : top right corner
  3. layerMinXMaxYCorner : bottom left corner
  4. layerMinXMinYCorner : top left corner

Also, check the documentation

https://developer.apple.com/documentation/quartzcore/calayer/2877488-maskedcorners