I am working on a swift app for iPhone. There is a modal view in my application that I want only to be in portrait view.
My question is, how do I programmatically force the phone to not allow rotation? In other words, I am looking for code that will not allow a modal view to be displayed in landscape mode (turning on portrait rotation lock).
This is just for 1 modal view, so I can't turn off rotation for the entire app, otherwise I would just disable rotation altogether.
I found code in my research here But it is in objective C, in case that helps. Thanks!
// Swift 2
You can paste these methods in the ViewController of each view that needs to be portrait:
Above code might not be working due to possibility if your view controller belongs to a navigation controller. If yes then it has to obey the rules of the navigation controller even if it has different orientation rules itself. A better approach would be to let the view controller decide for itself and the navigation controller will use the decision of the top most view controller.
Now inside your view controller we can
Hope it helps. Thanks
Note: This method allows rotation for a entire app.
OR
Make a ParentViewController for UIViewControllers in a project (Inheritance Method).
Associate every view controller's parent controller as UIappViewController.
My humble contribution (Xcode 8, Swift 3):
... on the AppDelegate. All the credits for Gandhi Mena: http://www.jairobjunior.com/blog/2016/03/05/how-to-rotate-only-one-view-controller-to-landscape-in-ios-slash-swift/
This will disable autorotation of the view:
Update
If app is in landscape mode and you show a view which must be showed in portrait mode, this will allow app to change it's orientation to portrait (of course when device will be rotated to such orientation).