Reputation: 119
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?
Upvotes: 6
Views: 7500
Reputation: 191
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.
Upvotes: 8
Reputation: 5712
For swift3.0
To restrict different orientations for different views You need to do following things :
In App delegate File :
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if self.window?.rootViewController?.presentedViewController is OtherViewController {
return UIInterfaceOrientationMask.All;
} else {
return UIInterfaceOrientationMask.Portrait;
}
}
Upvotes: 0
Reputation: 11
You can override the methods below
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.Portrait
}
Then, in your viewDidLoad, force the orientation by adding the code below
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
Upvotes: 0