JDM
JDM

Reputation: 893

App crashing when dismissing a view controller in portrait

I have viewcontroller X and viewcontroller Y.

viewController X presents Y, Viewcontroller X should only be shown in portrait mode but Y can rotate to whatever mode it wants.

When I dismiss viewController Y in landscape mode, I get the following error. In line:

[self dismissViewControllerAnimated:YES completion:nil];

error: Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'

In X Controller, I have:

-(BOOL)shouldAutorotate
{
    return NO;
}


- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

In Y controller I have:

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

This was running OK with ios 6 but is causing issues now in xcode /ios7. Now, even though I'm dismissing Y and getting back to X in landscape mode, I have it set to prefer portrait, so shouldn't it just force portrait mode regardless of my having the device in landscape?

Upvotes: 2

Views: 1739

Answers (3)

sust86
sust86

Reputation: 1870

Make sure to override preferredInterfaceOrientationForPresentation: UIInterfaceOrientation as it will inherit a value based on the rotation of your dismissing view controller. If this value then doesn't align with your view controllers supported interface orientation the app will crash.

    /// Returns `.portrait` as the default preferred interface orientation for presentation.
    ///
    /// - Important: It's important to explicitly set this value to a value which is aligned with `supportedInterfaceOrientations`. Not doing so can lead to crashes.
    /// In certain scenarios, if not set explicitly this value will be inherited from a dismissing view controller. The application will crash if the dismissing view controller was rotated to an orientation which isn't supported by
    /// this view controller. (*** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation 'landscapeRight' must match a supported
    /// interface orientation: 'portrait'!')
    override public var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .portrait
    }

Upvotes: 0

Ethandf
Ethandf

Reputation: 177

add this before dismiss the child viewController:

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];

suppose your parent is in portrait

Upvotes: 1

Mundi
Mundi

Reputation: 80273

Return YES in your shouldAutorotate method. Maybe you have been doing this in the wrong view controller.

Upvotes: 0

Related Questions