1
votes

I have a custom UICollectionViewCell with an oval(with a border) and a label. The cell will change its size based on the size of the label.

Why does my view get warped?

enter image description here

GOAL

enter image description here

1. create a prototype cell in Interface builder with a subclass of UIView with grey border. It has top, bottom, trailing, & leading constraint constant of 4.

2. I add label (not subview of bordered view explained above) with top, bottom, trailing, & leading constraint constant of 8.

3. I add IBOutlet of collectionViewFlowLayout so I can set estimated size (this is for cell resizing).

collectionViewFLowLayout.estimatedItemSize =
UICollectionViewFlowLayoutAutomaticSize

4. The rest is boilerplate like conforming to UICollectionViewDataSource

Here is Custom view class.

class CustomView: UIView {

    override func awakeFromNib() {
        super.awakeFromNib()
        self.layer.borderColor = UIColor.lightGray.cgColor
        self.layer.borderWidth = 1
        self.layer.cornerRadius = self.frame.height / 2
        self.clipsToBounds = true
    }

}
1
place a label inside a view with top,bottom,leading and trailing constraints and make the cornerRaduis of the view = it's height /2Sh_Khan
How you are setting corner radius to label?. show some codemayursinh zala
@mayursinhzala I only set corner radius of view subclass like this: self.layer.cornerRadius = self.frame.height / 2user6354073
@Sh_Khan didn't work. still warped.user6354073
Can u share that subclass of the viewSh_Khan

1 Answers

0
votes

Your corner radius is little too much, try decreasing it. This,

 self.frame.height / 2 

seems to be a wrong proportion, try increasing the denominator /3,/4 etc.