63

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.

Milad Faridnia
  • 8,038
  • 13
  • 63
  • 69
Nitesh
  • 1,359
  • 1
  • 15
  • 24
  • Please add some information about flowLayout.itemSize, collectionView.fram! Crash when rotate or viewDidLoad? – Alex Apr 15 '13 at 10:46
  • 10
    I have found out that this error comes only when i select the Scroll direction as horizontal. – Nitesh Apr 15 '13 at 10:46
  • 1
    It does not crash just the UI goes blank. This is the size of the cell. 300X460 Collection View frame- 300X480 Layout Frame-300X480 I have taken all these values from the xib I want the Collection view to scroll horizontally not vertically. – Nitesh Apr 15 '13 at 10:47
  • itemSize > CollecitonView.frame(height or width), check your itemSize and collecitonView.frame. – Alex Apr 15 '13 at 10:50
  • Is it because i have set the Scrolling as Horizontal? Cuz it seems to work fine in vertical scrolling – Nitesh Apr 15 '13 at 10:55
  • This post explains why this is happening and also provides solution: https://stackoverflow.com/a/39497850/4218640 – ElectroBuddha Jun 21 '17 at 16:45

15 Answers15

39

I found that, using storyboards, you have to go into the storyboard and click on the overall View Controller (the view should be highlighted in blue) and go to the Attributes Inspector (the fourth tab on the right side of the screen) and unchecking the "Under Top Bars", "Under Bottom Bars", and "Under Opaque Bars." This cleared up the issue for me, and it cleared it for my coworkers as well.

Connor Wagner
  • 517
  • 4
  • 7
29

I've been having some issues when the using a collection view to present full screen view controllers.

Basically at run time, it'll ask for the size of the item and simply return the size of the collection view:

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return self.collectionView.frame.size;
}
iwasrobbed
  • 45,197
  • 20
  • 140
  • 190
ninjaneer
  • 6,551
  • 8
  • 58
  • 103
28

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()
  }
Adrian
  • 14,925
  • 16
  • 92
  • 163
Sajjon
  • 5,608
  • 2
  • 41
  • 77
  • thank you! after hours of trying different suggestions this simple solution eliminated my layout warnings. – iksnae Oct 28 '15 at 15:20
  • This worked for me on a collectionView inside a `UIView`. Been at this for hours and this is the only thing that worked! I just overrode the `layoutSubviews` method. – Dominic Holmes Aug 14 '19 at 19:20
18

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.

Jonathan Crooke
  • 892
  • 7
  • 18
  • 2
    Why do you assign size of entire view to size of single cell ? – expert Oct 05 '13 at 07:00
  • 1
    I encountered this issue with full-screen cells. Not sure if the OP is doing the same, but this is the most obvious way that you would experience this issue. – Jonathan Crooke Oct 07 '13 at 09:56
10

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.

DarkoM
  • 315
  • 4
  • 5
3

That fixed my problem:

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
return CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height - 70);
}

U can see padding value from top and bottom on this screen:

enter image description here

Nik Kov
  • 10,605
  • 4
  • 56
  • 96
2

Be sure to set the right autoresizing mask for UICollectionView and UICollectionviewcell. This fixed my problem for iPhone 4 Simulator

2

For me I am using AKPickerView inside of a custom UIView which was giving me a slew of warnings like the ones mentioned here. All I did to eliminate the warnings was to Un-Check the box called 'AutoResize Subviews' in the Attributes Inspector for my custom UIView. That silenced the warnings so hard, I thought my logger stopped working.

enter image description here

Dave Levy
  • 754
  • 10
  • 16
2

What caused this for me was that I was trying to set my cells to the size of the superview rather than the size of the UICollectionView. Simply replaced the superview frame with the UICollectionView frame.

spogebob92
  • 1,334
  • 3
  • 22
  • 30
  • i was using CGSize(width: view.frame.width, height:view.frame.height ) as collectionview cell size , i changed to CGSize(width: collectionView.frame.width, height: collectionView.frame.height ) , then everything is okay now . (I put a collectiomview inside another collectionview cell ) – Richard Mao Aug 23 '19 at 02:48
1

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...

SeanT
  • 1,631
  • 1
  • 15
  • 22
1

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);


}
Maishi Wadhwani
  • 1,074
  • 15
  • 24
0

If you're loading a collectionView via a container view + UICollectionViewController, you may have set a height constraint on the container view which is constraining the height of the collection view itself. In my case, I found the collectionView.contentSize was taller than the constraint I had set on my container view.

Obviously this is a bit of an edge case but just in case you have a similar setup, I figured I'd add this comment.

the_dude_abides
  • 533
  • 4
  • 10
0

For me the solution was to set Estimate Size (in the storyboard size attributes pane) for the collection view to None.

John Scalo
  • 2,774
  • 1
  • 22
  • 30
0

It's complaining that the item height is bigger than the height of the collectionView. So it's sticking out. So you have to make the height the same or less. Like this:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.frame.height, height: collectionView.frame.height)
    
}

Using the height for the width makes it a square. This is assuming inset is 0 which it should be if you haven't done anything.

Tilo Delau
  • 103
  • 1
  • 1
  • 11
-3

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;
}
Gal Blank
  • 1,987
  • 2
  • 15
  • 18
  • 3
    This seems to be incomplete code. Where is return statement? Is it just CGSize(width, collectionView.frame.size.height) or have you changed it? – Nat Feb 28 '15 at 11:52