Force portrait mode when the presented view controller is disabled

I have a presented view controller that supports all orientations of the interface. However, the view controller should only support portrait mode.

So far so good.

However, in iOS8, when I dismiss the WHILE view controller in landscape mode, the landscape mode persists. And since I have shouldAutorotate

it installed in NO

, it never turns back.

The question is, how can I get the display VC to return to portrait?

Currently implemented this workaround:

- (BOOL)shouldAutorotate
{
  if ([self interfaceOrientation] != UIInterfaceOrientationPortrait)
  {
    return YES;
  }
  else
  {
    return NO;
  } 
}

      

This allows you to move the device around in the portrait and it stays there as the portrait self-portrait is disabled after it.

But it looks ugly until the user turns their phone.

How do I force it?

+3


source to share


2 answers


We had exactly the same problem. You can rotate it programmatically by code -

if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait) {
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}

      

There are 2 possible options -

1) before dismissing the presented viewController rotate to portrait if needed

2) after firing, rotate to the portrait in the viewDidAppear of the current viewController.



One problem is that you cannot pass a completion block, but you can use the following callback in iOS8:

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    if (self.needToDismissAfterRotatation)
        self.needToDismissAfterRotatation = NO;
        [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
            // dismiss
        }];
    }
}

      

By the way, in iOS8 apple changed the way the screen is rotated a lot, when the app rotates, the screen rotates, and all the elements in the UIWindow rotate as well, so when the presented viewController is rotated to landscape, the displaying viewController also rotates, even if it only supports portrait ...

We have been struggling with this problem for many days, finally we came up with a solution to put the presented viewController in a new UIWindow interface, and this way it keeps the displaying viewController in portrait all the time

example project for this: "modalViewController" in UIWindow


+1


source


This code will force the UI to revert to the portrait, assuming that the view controller you are trying to force the portrait to have was already the root view controller (if it was not already root, this code will restore the root view controller, but not any other controllers views that were clicked on it):

UIInterfaceOrientation orientation = [[UIApplication 
    sharedApplication] statusBarOrientation];

if (orientation != UIInterfaceOrientationPortrait) {

    // HACK: setting the root view controller to nil and back again "resets" 
    // the navigation bar to the correct orientation
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIViewController *vc = window.rootViewController;
    window.rootViewController = nil;
    window.rootViewController = vc;

}

      



This is not very pretty as it does a sharp jump after the top-level view controller has been fired, but better than leaving it in terrain.

0


source







All Articles