Question

I'm having trouble switching between two subclassed CollectionViewFlowLayouts.

I call the following method in my collectionViewController:

header:

@property (nonatomic, strong) PortraitFlowLayout *portraitFlowLayout;
@property (nonatomic, strong) LandscapeFlowLayout *landscapeFlowLayout;

Implementation:

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

    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    if (UIDeviceOrientationIsPortrait(toInterfaceOrientation)) {

        self.landscapeFlowLayout = nil;
        [self portraitFlowLayout];
        NSLog(@"Orientation portrait");

    } else {

        self.portraitFlowLayout = nil;
        [self landscapeFlowLayout];
        NSLog(@"Orientation landscape");
    }

    [self.collectionView.collectionViewLayout invalidateLayout];

}

and in the same collectionViewController:

- (LandscapeFlowLayout *)landscapeFlowLayout
{
    if (_landscapeFlowLayout == nil) {
        _landscapeFlowLayout = [[LandscapeFlowLayout alloc] init];

        self.collectionView.collectionViewLayout = _landscapeFlowLayout;


    }
    [self.collectionView.collectionViewLayout invalidateLayout];
    return _landscapeFlowLayout;
}

- (PortraitFlowLayout *)portraitFlowLayout
{
    if (_portraitFlowLayout == nil) {
        _portraitFlowLayout = [[PortraitFlowLayout alloc] init];

        self.collectionView.collectionViewLayout = _portraitFlowLayout;


    }
    [self.collectionView.collectionViewLayout invalidateLayout];
    return _portraitFlowLayout;
}

I know that both layout are valid, and working, since I'm pushing into this viewController form another viewCont, and I've tried to do it with both the landscape and the portrait layout, which works fine.

The problem arises when I change the orientation. The first orientation change is fine, and the layout change as it's supposed to. But when it's then rotated back (sometimes it will rotate back and forth a few times before crashing), it gives me the following error when I trace it with the Zombie template in Instruments:

enter image description here

enter image description here

How can I trace this error further? Or, fix the problem? Any help is greatly appreciated. Thanks in advance

EDIT The problem seems only to arise when rotating to portrait.

Chris

Was it helpful?

Solution

Try to use UIDeviceOrientationDidChangeNotification instead willRotateToInterfaceOrientation.

This lines from the Apple's docs:

To support an alternate landscape interface, you must do the following:

Implement two view controller objects. One to present a portrait-only interface, and the other to present a landscape-only interface. Register for the UIDeviceOrientationDidChangeNotification notification. In your handler method, present or dismiss the alternate view controller based on the current device orientation.

- (void)awakeFromNib
{
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientationChanged:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];



}

- (void)orientationChanged:(NSNotification *)notification
{

    //without selector event may be lost
    [self performSelector:@selector(updateFrameWithOrientation) withObject:nil afterDelay:0];

}

-(void) updateFrameWithOrientation{
    UIInterfaceOrientation deviceOrientation = [UIApplication sharedApplication].statusBarOrientation;

    if (UIDeviceOrientationIsLandscape(deviceOrientation))
    {


    }
    else if (UIDeviceOrientationIsPortrait(deviceOrientation))
    {


    }

    [self.collectionView reloadData];

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top