0
votes

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?

1

1 Answers

1
votes

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.