I am woking on iOS application in swift. I have used UITabBarController as a rootViewController. I have list of videos in one viewController. This viewController supports only portrait mode and user select a video then enter playerController using showViewController method, that can support both orientation (Portrait and Landscape mode). If video finished then playerController pop to video list controller.Every things is fine but user can rotate screen during the finishing the video(like remaining time 1 or 0 second) then video list viewController enter in Landscape mode. I have tried this code for set player orientation in portrait mode.
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
But not worked. How to fixed this issue.
To lock a viewController in a mode do the following:
In your AppDeletegate add:
Now go to each view and add the following in
viewWillAppear
:Portait mode only
All modes
Update
If you want to go from landscape back to portrait again when you change view/tab, add the following code
Swift 3.x version
Add this in your AppDelegate class
Add this in your viewController
Swift 4 version:
In your AppDelegate, add the following:
In each viewController add the following:
This will lock your view and rotate it.
You first must go to your general project settings and make sure your "Device Orientation" is set to Portrait (and optionally Upside Down)
Then In the info.pList file,
Check the key Supported interface orientations. Delete all its keys except the only portrait key. Review the below image for it.
This will force your app to run in portrait. Even when you close your player in landscape mode, your app will move back to portrait mode.