UICollectionView Layout Issue UICollectionView Layout Issue ios ios

UICollectionView Layout Issue


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.


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


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()  }