0

I have an app with several view controllers, most of which should be able to have any orientation, but one is required to be in Portrait. However, the app ignores the value I return from supportedInterfaceOrientations()

My code:

UINavigationController subclass:

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

    return [.Landscape, .Portrait]
}

override func shouldAutorotate() -> Bool {
    if let topViewController = self.topViewController {
        return topViewController.shouldAutorotate()
    }

    return true
}


UIViewController subclass

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

override func shouldAutorotate() -> Bool {
    return true
}

When I'm in a landscape-compatible view controller and go to the one which is supposed to be in portrait, nothing happens - the app does not autorotate to portrait.

I removed supported interface orientations from info.plist as well. Any ideas?

mag_zbc
  • 6,801
  • 14
  • 40
  • 62

1 Answers1

1

You can set them in AppDelegate.swift, inside this function

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

    return checkOrientationForVC(self.window?.rootViewController?)

}

And inside this function you can do something like :

   func checkOrientationForVC(viewController:UIViewController?)-> Int{

    if(viewController == nil){

        return Int(UIInterfaceOrientationMask.All.rawValue)//All means all orientation

    }else if (viewController is SignInViewController){ //Your View controller here

        return Int(UIInterfaceOrientationMask.Portrait.rawValue)

    }else{

        return checkOrientationForVC(viewController!.presentedViewController?)
    }
}

Also set orientation in Storyboard too under "Simulated Metrics" in "Orientation".You can also see this Stackoverflow's post.

Community
  • 1
  • 1
Munahil
  • 2,381
  • 1
  • 14
  • 24
  • that helped a little bit but not entirely - it still doesn't autorotate. When I switch to desired view controller, it's still in landscape, but when I rotate the device, it locks in portrait. shouldAutorotate() returns true both in view controller and navigation controller – mag_zbc Nov 18 '16 at 15:21