Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is the appropriate moment in rotation to change the Layout parameters of a UICollectionView

Hopefully a simple question for a relatively new UIKit control

I have a UICollectionView that has a viewLayout with a single row of cells that are the exact height of the UICollectionView bounds in Portrait mode

Therefore, when the iPad is flipped to Landscape mode, that row becomes taller than the screen itself, at which point the layout (almost) silently fails and complains that the row is taller than the bounds.

What is the ideal way to manipulate the characteristics of the viewLayout, particularly as it relates to responding to rotation in a ViewController?

like image 808
M. Ryan Avatar asked Sep 26 '12 16:09

M. Ryan


1 Answers

I'm working on a similar problem.

Currently, my UICollectionViewController has two instance variables of UICollectionViewFlowLayout, each with the appropriate insets for portait or landscape.

On rotation, I do this:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                               duration:(NSTimeInterval)duration{

    if (UIDeviceOrientationIsPortrait(toInterfaceOrientation)) {
        [_itemCollection setCollectionViewLayout:_portraitLayout];
        [_itemCollection reloadData];
    } else {
        [_itemCollection setCollectionViewLayout:_landscapeLayout];
        [_itemCollection reloadData];
    }
}

The only problem that I'm having is that it randomly crashes with exc_bad_access on setCollectionViewLayout randomly.

Something like the above might work for you. I'm not sure if this is the right way to do things. I have only recently started using UICollectionViews.

like image 157
acorscadden Avatar answered Oct 07 '22 18:10

acorscadden