Search code examples
iosobjective-ciphoneuiviewcontrolleruiinterfaceorientation

Presented UIViewController refuses to lay out with landscape orientation


I am updating a 5-year-old app (originally written for iOS 3!). I have made decent inroads in using autolayout and addressing deprecation warnings. But the old technique used for presenting a different view controller when the device is rotated no longer works reliably.

- (void)orientationChanged:(NSNotification *)notification {
            UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
            if (deviceOrientation == UIInterfaceOrientationLandscapeRight && !showingOtherVC) {
                // switch to other VC
                othervc.modalPresentationStyle = UIModalPresentationOverFullScreen;
                [self presentViewController:othervc animated:YES completion:nil];
                [self resignFirstResponder];
             }

The other view controller does appear, but it's laid out sideways, for a portrait screen, not landscape, even though the device is in a landscape orientation.

How can I update this in a reasonably easy way (i.e., not a rewrite in Swift, not restructuring the app with storyboards — which Xcode doesn't seem to facilitate via copy/paste)? And, for the benefit of others who may happen on this question, what would be the more correct way to achieve this result (new VC on orientation change) if I were writing this from scratch?

Thank you.


Solution

  • This was a really stupid error, but in case someone else makes it and ends up here, this was the problem.

    Instead of correctly returning the mask constant:

    -(UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return (UIInterfaceOrientationMaskLandscapeRight);
    }
    

    I was returning this other constant that autocomplete gave me:

    -(UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return (UIInterfaceOrientationLandscapeRight);
    }