1

I am supporting all orientations and autorotation in my iOS app, but I'd like certain view controllers to not show their views in landscape. I'm implementing shouldAutorotate method in such view controllers:

- (BOOL)shouldAutorotate
{
   return NO;
}

and it certainly prevents the view from being rotated to landscape when it is loaded or it appeared in portrait. But when the view controller is loaded or it appears being the device already in landscape, the view is shown in landscape.

How could I load/make the view appear in portrait from the beginning, regardless of the device's orientation?

Thanks

AppsDev
  • 12,319
  • 23
  • 93
  • 186
  • [Alan's Answer here](http://stackoverflow.com/questions/17466048/how-to-allow-only-single-uiviewcontroller-to-rotate-in-both-landscape-and-portra) is helpful – NSNoob Dec 08 '15 at 09:06

2 Answers2

1

You can use the method supportedInterfaceOrientations() in order to specify the orientation available for a specific view controller.

But, when your embed your view controllers in a navigation stack, the system calls shouldAutorotate and supportedInterfaceOrientations on your navigation controller, and the value returned from your view controllers are ignored.

I use a subclass of UINavigationController in order to get the supported orientations from the top view controller. You can also created such a class for UITabBarViewController

class SingleOrientationNavigationController: UINavigationController {

    //MARK : Orientation
    override func shouldAutorotate() -> Bool {
        if let topVC = topViewController {
            return topVC.shouldAutorotate()
        }
        return false
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        if let topVC = topViewController {
            return topVC.supportedInterfaceOrientations()
        }
        return UIInterfaceOrientationMask.Portrait
    }

    // MARK : Status bar
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        if let topVC = topViewController {
            return topVC.preferredStatusBarStyle()
        }
        return .Default
    }
}

Be careful, you can have some issues if the pushed view controllers got less rotation option than the previous one.

Let's consider two view controllers :

A : only portrait
B : all orientation

If a push is performed from A to B, then it will be ok, B can be rotating as it desire, and even if B is in landscape when it will pop, you'll go back to A in portrait.

But if a push is performed from B to A while B is in landscape, then A will be displayed as landscape too ! One workaround is to use modal view controllers instead of pushing it in this special case.

Michaël Azevedo
  • 3,874
  • 7
  • 31
  • 45
  • Thank you. I made some tests and I don't manage to make this work in iPhone 6 Plus... do you know if those devices require some additional setting? – AppsDev Dec 08 '15 at 16:37
0

It can be done by adding Initial interface orientation = Portrait (bottom home button) in .plist of your application.

plist image

Balaji Kondalrayal
  • 1,743
  • 3
  • 19
  • 38
  • Thank you. But this may affect the whole app, and I'd just want this behavior for some views – AppsDev Dec 08 '15 at 09:14
  • When the app is loading, it will appear in portrait. So the view which you dont want to rotate can use the function `-(NSUInteger)supportedInterfaceOrientations` and `- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation` – Balaji Kondalrayal Dec 08 '15 at 09:16
  • Thanks. I'm testing and that setting in the `.plist` file and it doesn't seem to work in an iPhone 6 Plus, I'm launching the app being the device in landscape and the views are shown in landscape, even the launch image... – AppsDev Dec 08 '15 at 16:30