UICollectionView Layout Issue

2019-02-01 15:30发布

I am using UICollectionView using the flow layout. I have made a custom UICollectionViewCell for the same. But on running the project the console keeps on throwing this error-

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.

I have made sure that the size of the cell is correct

Has anyone been able to resolve this issue. Thanks in advance.

Regards Nitesh

13条回答
一纸荒年 Trace。
2楼-- · 2019-02-01 16:25

I have same issue

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

I solved this issue by checking the values in section Insets.And for fix cell size I have used below code.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
CGFloat cellWidth =  [[UIScreen mainScreen] bounds].size.width - 20;
CGFloat cellHeight = [[UIScreen mainScreen] bounds].size.height - 120;

return CGSizeMake(cellWidth, cellHeight);


}
查看更多
Viruses.
3楼-- · 2019-02-01 16:27

None of the above fixes did it for me. I fixed it with this

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{CGFloat currentWidth = collectionView.frame.size.width;
UIEdgeInsets sectionInset = [(UICollectionViewFlowLayout *)collectionView.collectionViewLayout sectionInset]; //here the sectionInsets are always = 0 because of a timing issue so you need to force set width of an item a few pixels less than the width of the collectionView.

CGFloat width = currentWidth - 10;
}
查看更多
做自己的国王
4楼-- · 2019-02-01 16:28

If you are using storyboards and auto layout, debugging this kind of problems is really hard...

I had similar problem when trying to display UICollectionViewCell fullscreen on iPhone.

Most of the time issue is with the size of the cell set in

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)

or directly on flowLayout.itemSize.

But... try:

  1. Select the ViewController:

Selecting View Controller

  1. Then uncheck Extend Edges options:

Disable Extend Edges Options

And now try setting your auto layout constraints.

Good luck.

查看更多
我只想做你的唯一
5楼-- · 2019-02-01 16:28

I just ran into the same error message, but for me the issue was that when I received new data from the api and tried to refresh my collectionView, the method that called [collectionView reloadData] wasn't calling it on the main thread.

Hope this helps someone...

查看更多
乱世女痞
6楼-- · 2019-02-01 16:29

Had this issue myself a few times when trying to create collection views with fullscreen cells. My problem was caused by laying out for 4" screen in IB/Storyboard and specifying 320*568 for item size, but running in the simulator using 3.5" screen, which has a height of 480. The solution is to specify your item size in code with something like:

UICollectionViewFlowLayout *layout = (id) self.collectionView.collectionViewLayout;
layout.itemSize = self.collectionView.frame.size;

This ensures that the cell size is set correctly at runtime.

查看更多
Bombasti
7楼-- · 2019-02-01 16:30

I know this is a very late reply, but I have also experienced this.

Inside my view controller, let's call it MyViewController I have a UICollectionView that is using custom UICollectionViewCells. I'm implementing the method collectionView:layout:sizeForItemAtIndexPath where I am returning a item size that is dependent on the height of the UICollectionView.

MyViewController is made in a storyboard using autolayout to control the height of the UIViewController.

The cells look fine when in portrait mode, but did not when in landscape mode.

I solved my issues by invalidating the UICollectionViewLayout of my UICollectionView inside the viewWillLayoutSubviews method.

- (void)viewWillLayoutSubviews {
    [self.myCollectionView.collectionViewLayout invalidateLayout];
}

Earlier I had tried to invalidate the layout inside willAnimateRotationToInterfaceOrientation:duration, but it didn't work. The reason for this is probably that the sizes for all the views are not calculated yet, so we have to wait until autolayout has finished its magic. I refer to this SO thread.

Update for Swift 4.0:

  override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    self.myCollectionView.collectionViewLayout.invalidateLayout()
  }
查看更多
登录 后发表回答