100

I am using an UICollectionView with UICollectionViewFlowLayout.

I set the size of each cell through the

collectionView:layout:sizeForItemAtIndexPath:

When switching from portrait to landscape, I would like to adjust the size of each cell to completely fit the size of the CollectionView, without leaving padding space between cells.

Questions:

1) How to change the size of a cell after a rotation event?

2) And, even better, how to make the layout where the cells always fit the entire size of the screen?

Blaszard
  • 27,599
  • 40
  • 143
  • 217
Fmessina
  • 3,491
  • 3
  • 28
  • 34
  • followben's answer is currently accepted one, but it has some issues: it will throw a console error and the animation to the new size will not happen correctly. See my answer for the correct implementation. – memmons Mar 20 '14 at 16:46
  • @MichaelG.Emmons: your answer will work better where a collection view only displays a single full-sized cell at a time. In that case, it makes sense UIKit complains on rotation as it'll be querying `sizeForItemAtIndexPath` before calling `didRotateFromInterfaceOrientation`. However, for a collection view with multiple cells on screen, invalidating the layout before rotation causes a nasty, visible jump in size before the view rotates. In that instance, I believe my answer is still the most correct implementation. – followben Mar 20 '14 at 23:19
  • @followben Agree that each has their issues in different use cases. In this case though, the OP indicates `I would like to adjust the size of each cell to **completely fit the size of the CollectionView**` which is exactly the solution proposed in my answer. If you could include my solution in your answer and make note of which approach works best under which conditions that would be good enough for me. – memmons Mar 21 '14 at 15:06

6 Answers6

200

1) You could maintain and swap out multiple layout objects, but there's a simpler way. Just add the following to your UICollectionViewController subclass and adjust the sizes as required:

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{    
    // Adjust cell size for orientation
    if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        return CGSizeMake(170.f, 170.f);
    }
    return CGSizeMake(192.f, 192.f);
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.collectionView performBatchUpdates:nil completion:nil];
}

Calling -performBatchUpdates:completion: will invalidate the layout and resize the cells with animation (you can just pass nil to both block params if you've no extra adjustments to perform).

2) Instead of hardcoding the item sizes in -collectionView:layout:sizeForItemAtIndexPath, just divide the height or width of the collectionView's bounds by the number of cells you want to fit on screen. Use the height if your collectionView scrolls horizontally or the width if it scrolls vertically.

followben
  • 8,585
  • 4
  • 35
  • 39
  • How do you handle the orientation changing while focus is on another view controller then focus is switched back to the original view controller. Currently my cells retain the size determined by the orientation they were left in but the new orientation. – Steve Moser Jan 25 '13 at 22:14
  • nm, I just invoked [self.collectionView performBatchUpdates:nil completion:nil]; during viewDidAppear – Steve Moser Jan 25 '13 at 22:15
  • 3
    why not just call `[self.collectionView.collectionViewLayout invalidateLayout];`? – user102008 May 15 '13 at 00:56
  • 7
    @user102008 Calling `invalidateLayout` will re-draw the cells with the correct size, but `-performBatchUpdates:completion:` will animate the changes, which looks a lot nicer IMHO. – followben May 15 '13 at 04:48
  • 4
    In my case the code doesn't resize the collectionviewcell until I scroll the screen. – newguy Aug 06 '13 at 01:30
  • 9
    `didRotateFromInterfaceOrientation` deprecated in iOS 8 – János Sep 03 '14 at 08:18
  • 8
    On iOS8, I'm calling `coordinator.animateAlongsideTransition` in `viewWillTransitionToSize`, and calling `performBatchUpdates` within its `animation` block. This gives a nice animation effect. – Mike Taverne Feb 20 '15 at 09:40
  • I had to add onto this re-scrolling the the proper cell when the items are the entire screen. – CWitty Mar 25 '15 at 14:46
33

If you don't want additional animations brought by performBatchUpdates:completion:, just use invalidateLayout to force the collectionViewLayout to reload.

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [self.collectionView.collectionViewLayout invalidateLayout];
}
Hai Feng Kao
  • 4,799
  • 2
  • 24
  • 37
13

I solved using two UICollectionViewFlowLayout. One for portrait and one for landscape. I assign them to my collectionView dinamically in -viewDidLoad

self.portraitLayout = [[UICollectionViewFlowLayout alloc] init];
self.landscapeLayout = [[UICollectionViewFlowLayout alloc] init];

UIInterfaceOrientation orientationOnLunch = [[UIApplication sharedApplication] statusBarOrientation];

if (UIInterfaceOrientationIsPortrait(orientationOnLunch)) {
    [self.menuCollectionView setCollectionViewLayout:self.portraitLayout];
} else {
    [self.menuCollectionView setCollectionViewLayout:self.landscapeLayout];
}

Then I simply modified my collectionViewFlowLayoutDelgate Methods like this

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGSize returnSize = CGSizeZero;

    if (collectionViewLayout == self.portraitLayout) {
        returnSize = CGSizeMake(230.0, 120.0);
    } else {
        returnSize = CGSizeMake(315.0, 120.0);
    }

    return returnSize;
}

Last I switch from a layout to one other on rotation

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    if (UIInterfaceOrientationIsPortrait(fromInterfaceOrientation)) {
        [self.menuCollectionView setCollectionViewLayout:self.landscapeLayout animated:YES];
    } else {
        [self.menuCollectionView setCollectionViewLayout:self.portraitLayout animated:YES];
    }
}
shawnwall
  • 4,331
  • 1
  • 24
  • 35
Fiveagle
  • 946
  • 7
  • 11
8

There's a simple way to set collection view cell size:

UICollectionViewFlowLayout *layout = (id) self.collectionView.collectionViewLayout;
layout.itemSize = CGSizeMake(cellSize, cellSize);

Not sure if it's animatable though.

Dannie P
  • 4,096
  • 3
  • 26
  • 46
  • i had to use this inside of viewWillLayoutSubviews with Hai Feng Kao's answer: http://stackoverflow.com/a/14776915/2298002 – greenhouse May 20 '15 at 22:32
7

Swift : Collection View Cell that is n% of screen height

I needed was a cell that was a certain percentage of views height, and would resize with device rotation.

With help from above, here is the solution

override func viewDidLoad() {
    super.viewDidLoad()
    automaticallyAdjustsScrollViewInsets = false
    // if inside nav controller set to true
}

override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
    collectionViewLayout.invalidateLayout()
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSize(width: 100, height: collectionView.frame.height * 0.9)
}
DogCoffee
  • 18,799
  • 9
  • 81
  • 114
2

If you are using autolayout.You just need to invalidate layout in your view controller when rotating and adjust offset in your flow layout subclass.

So, In your view controller -

override func willRotateToInterfaceOrientation(toInterfaceOrientation:     UIInterfaceOrientation, duration: NSTimeInterval) {
    self.galleryCollectionView.collectionViewLayout.invalidateLayout()
}

And in your FlowLayout Subclass

override func prepareLayout() {
    if self.collectionView!.indexPathsForVisibleItems().count > 0{
    var currentIndex : CGFloat!
    currentIndex = CGFloat((self.collectionView!.indexPathsForVisibleItems()[0] as! NSIndexPath).row)
    if let currentVal = currentIndex{
        self.collectionView!.contentOffset = CGPointMake(currentIndex * self.collectionView!.frame.width, self.collectionView!.contentOffset.y);
    }   
    }     
}
toofani
  • 1,442
  • 12
  • 16
  • 1
    This works great! `willRotateToInterfaceOrientation` is deprecated in iOS 8 but `viewWillTransitionToSize` works just as well for this. – keegan3d Sep 07 '15 at 01:02