Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain presenting view controller's orientation when dismissing modal view controller?

I have this app I am working on and I need ALL my view controllers but one to be in portrait. The single one view controller that is special I need it to be able to rotate to whatever orientation the phone is in.

To do that I present it modally (not embedded in a NavigationController)

So (for example) my structure is like this:

  • window - Portrait
    • root view controller (UINavigationController - Portrait)
      • home view controller (UIViewController - Portrait)
        • details view controller (UIViewController - Portrait)
        • .
        • .
        • .
        • modal view controller (UIVIewController - All)

Now when ever I dismiss my modal view controller in a landscape position my parent view controller is ALSO rotated even though it doesn't support that orientation.

All UIViewControllers and UINavigaionControllers in the app inherit from the same general classes which have these methods implemented:

override func supportedInterfaceOrientations() -> Int {     return Int(UIInterfaceOrientationMask.Portrait.toRaw()) } 

My modal view controller overrides this method once again and it looks like this:

override func supportedInterfaceOrientations() -> Int {     return Int(UIInterfaceOrientationMask.All.toRaw()) } 

Update 1

It looks like this is happening only on iOS8 Beta. Does someone know if there is something that changed regarding view controller's rotation or is this just a bug in the beta?

like image 248
Mihai Fratu Avatar asked Jun 15 '14 12:06

Mihai Fratu


People also ask

How do you dismiss a modal view controller?

According to the View Controller Programming guide for iPhone OS, this is incorrect when it comes to dismissing modal view controllers you should use delegation. So before presenting your modal view make yourself the delegate and then call the delegate from the modal view controller to dismiss.


1 Answers

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if ([self.window.rootViewController.presentedViewController isKindOfClass: [SecondViewController class]]) {     SecondViewController *secondController = (SecondViewController *) self.window.rootViewController.presentedViewController;      if (secondController.isPresented)         return UIInterfaceOrientationMaskAll;     else return UIInterfaceOrientationMaskPortrait; } else return UIInterfaceOrientationMaskPortrait; } 

And for Swift

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {      if self.window?.rootViewController?.presentedViewController? is SecondViewController {          let secondController = self.window!.rootViewController.presentedViewController as SecondViewController          if secondController.isPresented {             return Int(UIInterfaceOrientationMask.All.toRaw());         } else {             return Int(UIInterfaceOrientationMask.Portrait.toRaw());         }     } else {         return Int(UIInterfaceOrientationMask.Portrait.toRaw());     }  } 

For more details check this link

like image 127
ZaEeM ZaFaR Avatar answered Sep 27 '22 22:09

ZaEeM ZaFaR