Why does UICollectionView log an error when the ce

2020-02-02 10:56发布

I have a UICollectionViewController using a UICollectionViewFlowLayout where my itemSize is the size of the UICollectionView. Basically, this is a line layout of cells where each cell is fullscreen and scrolls horizontally.

In my UICollectionViewFlowLayout subclass, I have overridden prepareLayout as follows:

- (void)prepareLayout {
    self.itemSize = self.collectionView.frame.size;
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.pagingEnabled = YES;
    self.minimumLineSpacing = 0.0;
    self.minimumInteritemSpacing = 0.0;
    self.sectionInset = UIEdgeInsetsZero;
    self.footerReferenceSize = CGSizeZero;
    self.headerReferenceSize = CGSizeZero;
}

The UICollectionViewController is very basic returning 10 items in one section. I've included a sample project on GitHub for more detail.

Everything appears to be set up correctly. It looks right in the simulator and on the device but, when the collection view is displayed, there is an error logged to the console:

the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less that the height of the UICollectionView minus the section insets top and bottom values.

Note also that the collection view controller in my example is in a navigation controller and while that doesn't look particularly necessary in the example, in my real-world case I need the collection view in a navigation controller.

8条回答
我想做一个坏孩纸
2楼-- · 2020-02-02 11:52

I had similar issue.

After load cell which is full width and some height of screen. on some condition I changed the height of cell then I was getting the same error

to fix this

I used

   func updateHeightPerRatio(with image:UIImage) {
    let ratio = collectionView.bounds.width / image.size.width
    constHeightCollectionView .constant =  ceil(image.size.height * ratio)
    collectionView.reloadData()
    collectionView.performBatchUpdates({
        collectionView.layoutIfNeeded()
    }) { (completed) in
        self.collectionView.reloadData()
        self.layoutIfNeeded()
    }
}

Solution is reload data then perform batchupdate with that collection view re -calculate the frames . after that reload collectionview again it will apply calculated frames to cell

And now there is no log for issue now.

Hope it is helpful

查看更多
够拽才男人
3楼-- · 2020-02-02 11:53

Like Stunner, I had the same problem when rotating from landscape (picture using full width) to portrait mode. His suggestion was the only one which really helped.

Attached the code with latest Swift for his ObjC example ... and as a bonus, my code to find the center cell of the collection view. Works quite nice ;-)

/**
 -----------------------------------------------------------------------------------------------

 viewWillTransition()

 -----------------------------------------------------------------------------------------------
 */
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

    // find cell in the center of the screen and store it in a global variable
    let center = self.view.convert((self.collectionView!.center), to: self.collectionView)

    // get the indexPath for the cell in the center of the current screen
    if let index = collectionView!.indexPathForItem(at: center) {

        // store it in a class property
        self.indexPathOfCenterCell = index
    }

    // force recalculation of margins, borders, cell sizes etc.
    self.collectionView?.collectionViewLayout.invalidateLayout()

    // inform UIKit about it    
    super.viewWillTransition(to: size, with: coordinator)
}
查看更多
登录 后发表回答