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)
bmjohns -> You are my life saviour. That is the only working solution (With the AppUtility struct)
I've created this class:
and followed your instructions, and everything works perfectly for Swift 3 -> xcode version 8.2.1
This is a generic solution for your problem and others related.
1. Create auxiliar class UIHelper and put on the following methods:
2. Create a Protocol with your desire behavior, for your specific case will be portrait.
protocol orientationIsOnlyPortrait {}
Nota: If you want, add it in the top of UIHelper Class.
3. Extend your View Controller
In your case:
4. In app delegate class add this method:
Final Notes:
As of iOS 10 and 11, iPad supports Slide Over and Split View. To enable an app in Slide Over and Split View,
Requires full screen
must be unchecked. That means the accepted answer cannot be used if the app wants to support Slide Over and Split View. See more from Apple's Adopting Multitasking Enhancements on iPad here.I have a solution that allows (1) unchecking
Requires full screen
, (2) just one function to be implemented inappDelegate
(especially if you don't want to / can't modify the target view controllers), and (3) avoid recursive calls. No need of helper class nor extensions.appDelegate.swift (Swift 4)
Edit
@bmjohns pointed out that this function is not called on iPad. I verified and yes it was not called. So, I did a bit more testing and found out some facts:
Requires full screen
because I want to enable Slide Over and Slide View on iPad. That requires the app to support all 4 orientation for iPad, in Info.plist:Supported interface orientations (iPad)
.My app works same way as Facebook: on iPhone, most of the time it is locked to portrait. When viewing image in full screen, allows users to rotate landscape for better view. On iPad, users can rotate to any orientation in any view controllers. So, the app looks nice when iPad is stand on Smart Cover (landscape left).
For iPad to call
application(_:supportedInterfaceOrientationsFor)
, in Info.plist, only keep portrait for iPad. The app will lose Slide Over + Split View ability. But you can lock or unlock the orientation for any view controller, in just one place and no need to modify ViewController class.Finally, this function get called on view controller's life cycle, when view is displayed/removed. If your app need to lock/unlock/change orientation in other time, it might not work
Create new extension with
Thanks to @bmjohn's answer above. Here is a working Xamarin / C# version of the that answer's code, to save others the time of transcription:
AppDelegate.cs
Static OrientationUtility.cs class:
View Controller:
A bunch of great answers in this thread, but none quite matched my needs. I have a tabbed app with navigation controllers in each tab, and one view needed to rotate, while the others needed to be locked in portrait. The navigation controller wasn't resizing it's subviews properly, for some reason. Found a solution (in Swift 3) by combining with this answer, and the layout issues disappeared. Create the struct as suggest by @bmjohns:
Then subclass UITabBarController:
Don't forget to change the class of the TabBarController in the storyboard to the newly created subclass.