Subview frame is incorrect when creating UICollectionViewCell

后端 未结 4 1871
醉酒成梦
醉酒成梦 2020-12-24 11:17

The problem

I created a UICollectionViewController with a custom UICollectionViewCell. The custom cell contains a large and rectangular UIView (name

4条回答
  •  粉色の甜心
    2020-12-24 11:57

    You can get the final frames of your cell by overriding layoutIfNeeded() in your custom Cell class like this:

    override func layoutIfNeeded() {
        super.layoutIfNeeded()
        self.subView.layer.cornerRadius = self.subView.bounds.width / 2
    }
    

    then in your UICollectionView data Source method cellForRowAtIndexPath: do this:

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CustomCollectionViewCell
    cell.setNeedsLayout()
    cell.layoutIfNeeded()
    

提交回复
热议问题