I'm a newbie in Swift and i have a problem locking the orientation to portrait in a viewController. Actually i have locked it using this code in my Custom Navigation Controller
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if (self.visibleViewController is ViewController)
{
return UIInterfaceOrientationMask.Portrait
}
return .All
}
Everything works fine and the ViewController is locked to portrait.The problem is when return to this controller from another in landscape mode. if i return to ViewController (pressing back from the NextViewController) in landscape then the ViewController appeared in landscape. Any suggestion?
Set the supportedInterfaceOrientations property of specific UIViewControllers like this: class MyViewController: UIViewController { var orientations = UIInterfaceOrientationMask. portrait //or what orientation you want override var supportedInterfaceOrientations : UIInterfaceOrientationMask { get { return self.
Historically, if you wanted to restrict your iOS app to specific device orientations, you would check or uncheck the various “Device Orientation” options in your project settings. You can find these by selecting your Xcode Project > App Target > “General” tab. Xcode project "Device Orientation" options.
From the main screen, slide the top of the screen down. Then, slide the top of the screen down again. Tap the "Portrait" icon to enable landscape mode, or tap the "Auto rotate" icon to disable it. Note: When "Auto rotate" is enabled, the interface will rotate when you hold the phone horizontally.
Step 1: Open the screen in which you want to set the orientation. Step 2: Add the init() method and then call the SystemChrome. setPreferredOrientations method to set only landscape mode. Step 3: Add the dispose method and then call the SystemChrome.
In swift 3 this solution works
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if self.window?.rootViewController?.presentedViewController is LockedViewController {
return UIInterfaceOrientationMask.portrait
} else {
return UIInterfaceOrientationMask.all
}
}
Change LockedViewController
to match the controller you would like locked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With