Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXC_BAD_ACCESS on setCollectionViewLayout

If I change the layout of a UICollectionView during an orientation change, I get EXC_BAD_ACCESS after a few rotations.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    UICollectionViewLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.myCollectionView.collectionViewLayout = layout; // This causes EXC_BAD_ACCESS after a few rotations
}

Is this a UICollectionView bug? Or am I doing something wrong? If it's a bug, is there any way to change the whole layout during an orientation change?

I noticed that the same problem is mentioned in this answer. Debugging with NSZombies does not produce additional information.

like image 651
hpique Avatar asked Nov 13 '22 18:11

hpique


1 Answers

Not quite sure it has something to do but the Release Notes for iOS 6 say:

The willRotateToInterfaceOrientation:duration:, willAnimateRotationToInterfaceOrientation:duration:, and didRotateFromInterfaceOrientation: methods are no longer called on any view controller that makes a full-screen presentation over itself—for example, presentViewController:animated:completion:

You should make sure that your apps are not using these methods to manage the layout of any subviews. Instead, they should use the view controller’s viewWillLayoutSubviews method and adjust the layout using the view’s bounds rectangle.

Since you are using UICollectionView maybe it has something to do that you are using this method to change its layout.

like image 155
jere Avatar answered Dec 03 '22 11:12

jere