At some point you may want to restrict certain device orientations to certain view controllers. Its quite easy to do from your App Delegate.
First add a new variable to your AppDelegate.swift file:
var allowableOrientations: UIInterfaceOrientationMask = .All
Next, implement the application:supportedInterfaceOrientationsForWindow function in the same file:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { return allowableOrientations; }
This will be called whenever you rotate the device to determine if that orientation is allowed. It doesn’t, however, force a rotation. We can implement another function in the same file to handle setting the allowableOrientations file and making sure the current orientation is one of those:
func setAllowableOrientations(orientationMask:UIInterfaceOrientationMask, defaultOrientation:UIInterfaceOrientation) { self.allowableOrientations=orientationMask; if ((orientationMask.rawValue & UInt(UIDevice.currentDevice().orientation.rawValue)) == 0) { UIDevice.currentDevice().setValue(defaultOrientation.rawValue, forKey: "orientation") } }
To use it, simply call this function from your View Controller’s viewDidLoad function, similar to this:
(UIApplication.sharedApplication().delegate as! AppDelegate).setAllowableOrientations(.Landscape,defaultOrientation: .LandscapeRight)
That’s it!