1

Was having some issues with UICollectionView and spacing of the cells. I figured out how to change the spacing and works perfectly for my portrait view but not for my landscape view. I tried this to differentiate:

//set minimum spacing
if(UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)){

    layout.minimumLineSpacing = 100.0f;
}
else{

    layout.minimumLineSpacing = 40.0f;
}

But unfortunately it doesn't change when I switch from portrait to landscape. Can anyone tell me how I can achieve this?

Zayd Bhyat
  • 101
  • 1
  • 2
  • 19
  • You need to reload your collection view if you want to call the datasource or delegate method of collection view – Rajat Nov 08 '16 at 09:07
  • This should answer your question: [http://stackoverflow.com/questions/18988028/how-do-i-define-the-size-of-a-collectionview-on-rotate](http://stackoverflow.com/questions/18988028/how-do-i-define-the-size-of-a-collectionview-on-rotate) – Ostap Horbach Nov 08 '16 at 09:11
  • The problem isnt the size of the cells but rather the spacing when its in landscape view. I need to have each cell alone and then paged to go to the next cell – Zayd Bhyat Nov 08 '16 at 09:14
  • first call [layout invalidateLayout] and then reload collection view. – kaushal Nov 08 '16 at 10:42

2 Answers2

0

Try this

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [yourcollectionView performBatchUpdates:nil completion:nil];
}
KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • :/... I should have mentioned I already call this function. I use it when I change the size of my cells. The above code isnt in its own function but in a function called. `- (void)loadView` should I have `layout.minimumLineSpacing` in its own function that returns the minimum spacing? – Zayd Bhyat Nov 08 '16 at 09:12
  • this is for reload your collection view while orientation . – KKRocks Nov 08 '16 at 09:14
  • Yes I understand. When I rotate the device my **Cells** in my **CollectionView** change in size but not the **Spacing**. maybe i should try to move the code to its own function that returns the line spacing? – Zayd Bhyat Nov 08 '16 at 09:16
0

I figured it out, it was exactly as I suspected. I moved the code to create the spacing into its own function and then simply return the values based on the orientation. The code is as follows:

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)
collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

if(UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)){

    NSLog(@"Changed to landscape Spacing");
    return 100.0f;
}
else{

    return 40.0f;
}

}

Thanks for the help guys, your input helped me find a solition, sometimes discussing the problem with someone helps me think

Zayd Bhyat
  • 101
  • 1
  • 2
  • 19
  • you should not use both layout.minimumLineSpacing and collectionView:layout delegate call. It will overwrite the older one. – kaushal Nov 08 '16 at 10:46
  • So you mean take out the line `layout.minimumLineSpacing = 40.0f` in the `loadView` function? I made sure I took that out before adding the new function – Zayd Bhyat Nov 08 '16 at 11:05