0

My application allows all four orientations, but the rootviewcontroller should only allow portrait orientation.

I override the supportedInterfaceOrientations method in my rootviewcontroller, but if the device is held in landscape orientation when the app launches then the view controller displayed incorrectly in landscape orientation even though only portrait orientation is allowed. This is an iOS 8 specific issue.

override func supportedInterfaceOrientations() -> Int {
    return UIInterfaceOrientation.Portrait.rawValue
}
MobileDev98
  • 63
  • 1
  • 8

2 Answers2

1

In ViewController:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

Swift version:

override func shouldAutorotate() -> Bool {
    return true
}
override func supportedInterfaceOrientations() -> Int {
    return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
ljk321
  • 16,242
  • 7
  • 48
  • 60
  • I need to select all orientations because a couple view controllers require landscape orientation. – MobileDev98 Jan 26 '15 at 00:11
  • @MobileDev98 I'm thinking you are using `supportedInterfaceOrientaions` wrong. You should use `UIInterfaceOrientationMask.Portrait` instead of `UIInterfaceOrientation.Portrait`. Check out my new edit. – ljk321 Jan 26 '15 at 00:59
  • override func supportedInterfaceOrientations() -> Int{ return Int(UIInterfaceOrientationMask.Portrait.rawValue)|Int(UIInterfaceOrientationMask.PortraitUpsideDown.rawValue) } – MobileDev98 Jan 26 '15 at 01:10
  • override func shouldAutorotate() -> Bool { return true } – MobileDev98 Jan 26 '15 at 01:11
  • supportedInterfaceOrientations method is getting called but not shouldAutorotate – MobileDev98 Jan 26 '15 at 01:11
  • @MobileDev98 Won't `application.statusBarOrientation` affect all view controllers? – ljk321 Jan 26 '15 at 02:09
0

For me implementing AppDelegate function application supportedInterfaceOrientationsForWindow did the trick

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int {

    if let viewController = self.window?.rootViewController?.presentedViewController as? PortraitViewController{

        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    }else{

        return Int(UIInterfaceOrientationMask.All.rawValue)
    }

}

where PortraitViewController should be replaced with the name of your root view controller class

Zell B.
  • 10,266
  • 3
  • 40
  • 49