Since my app got support for all orientation. I would like to lock only portrait mode to specific UIViewController.
(e.g. Assume it was Tabbed Application and when SignIn View appear modally, I only want that SignIn View to the portrait mode only no matter how the user rotate the device or how the current device orientation will be)
Actual tested Solution for this.In my example I need my whole app should be in portrait mode, but only one screen's orientation should be in landscape mode.
Code in AppDelegate as above answers described.
Then write down this code before your landscape orientation viewcontroller will be presented/push.
Then write down this code in actual viewcontroller(For landscape view)
To set Landscape orientation to all view of your app & allow only one view to All orientations (to be able to add camera roll for example):
In AppDelegate.swift:
In: didFinishLaunchingWithOptions
Elsewhere in AppDelegate.swift:
Then in the view that segue to the one you want to be able to have All orientations:
Last thing is to tick Device orientation: - Portrait - Landscape Left - Landscape Right
Add this code to force portrait and lock it:
In your AppDelegate - set supportedInterfaceOrientationsForWindow to whatever orientations you want the entire application to support:
Swift 4
AppDelegate
Your ViewController Add Following line if you need only portrait orientation. you have to apply this to all ViewController need to display portrait mode.
and that will make screen orientation for others Viewcontroller according to device physical orientation.
Things can get quite messy when you have a complicated view hierarchy, like having multiple navigation controllers and/or tab view controllers.
This implementation puts it on the individual view controllers to set when they would like to lock orientations, instead of relying on the App Delegate to find them by iterating through subviews.
Swift 3 & 4
In AppDelegate:
In some other global struct or helper class, here I created AppUtility:
Then in the desired ViewController you want to lock orientations:
If iPad or Universal App
Make sure that "Requires full screen" is checked in Target Settings -> General -> Deployment Info.
supportedInterfaceOrientationsFor
delegate will not get called if that is not checked.