Subview frame is incorrect when creating UICollectionViewCell

后端 未结 4 1854
醉酒成梦
醉酒成梦 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:45

    I had the same issue with a UICollectionViewCell using auto layout constraints.

    I had to call layoutIfNeeded before I was configuring my subview that relied on the views frame width.

    0 讨论(0)
  • 2020-12-24 11:47

    Ok, I understand now that the cell is created before auto layout defines its frames. That is the reason why at the moment of creation the bounds are wrong. When the cells are reused the frames have been already corrected.

    I was having this problem while creating a custom UIView that placed some layers and subviews in specific coordinates. When instances of this UIView were created, the placement of the subviews were all wrong (because auto layout hadn't kick off yet).

    I found out that instead of configuring the view subviews on init(coder: aCoder) I had to override the method layoutSubviews(). This is called when auto layout asks each view to layout its own subviews, so at this point at least the parent view has the correct frame and I can use it for laying the subviews correctly.

    Probably if I had used constraints on the custom view code instead of dealing myself with frame sizes and positioning then the layout would have been done properly and it wouldn't be necessary to override layoutSubviews().

    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2020-12-24 11:58

    Had this issue with Core Graphics drawing in iOS 10, Swift 3.0.1.

    Add this method to UICollectionView subclass:

    override func didMoveToSuperview() {
        super.didMoveToSuperview()
        setNeedsLayout()
        layoutIfNeeded()
    }
    

    My problem was that Core Graphics shapes were not calculated properly, because a layoutSubviews() wasn't called.

    0 讨论(0)
提交回复
热议问题