Can't force rotate of orientation in landscape mode - ios

My entire app is portrait mode. However, one screen needs to be able to rotate to landscape (for playing video). I am able to enable all orientation rotations with no problem via #Jonathan Danek's answer.
In my AppDelegate I have:
static var orientationLock = UIInterfaceOrientationMask.portrait
and subsequently:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}
So to enable rotation for all orientations, I do the following as the video view-controller is presented:
AppDelegate.orientationLock = UIInterfaceOrientationMask.all
UINavigationController.attemptRotationToDeviceOrientation()
This works great 👍 ..the problem is when I try to change it back to only portrait mode.
I am trying this as the video view-controller is dismissed:
AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
UINavigationController.attemptRotationToDeviceOrientation()
..but it does not trigger the above AppDelegate's supportedInterfaceOrientationsForWindow function if the video view-controller is currently rotated to Landscape. What's interesting is that it will trigger the function if the video view-controller is currently rotated to Portrait.
So I'm wondering how I can set my one view-controller to have all orientations, but then change it back to just portrait?

Have you tried to overwrite supportedInterfaceOrientations in your controller? That might help.
Example:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.all
}
Returns all of the interface orientations that the view controller supports.
Apple Doc - supportedInterfaceOrientations
Swift 5.1 short version:
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
.all
}

Related

Lock the orientation of the app in a specific View Controller in Swift

I'm working on an app that requires locking the orientation in each view controller.
I read this article, but it did not work for me.
In the project settings, I set the device orientation to Portrait, Landscape Left, and Landscape right.
I have two view controllers, VC1 and VC2, and I want to lock the VC1's orientation vertically only. I want to lock the VC2's orientation, on the other hand, horizontally only.
Then I added the following code in VC1.
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait //return the value as per the required orientation
}
override var shouldAutorotate: Bool {
return false
}
But, I can still able to rotate the VC1 horizontally...
Side note: VC1 contains a navigation bar and a tab bar.
If you know how to solve this issue, please let me know...
You can force orientation with few steps:
Firstly, In your AppDelegate define a orientation property and conform supportedInterfaceOrientationsFor
var orientationLock = UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.orientationLock
}
Then declare utility struct to set orientation using KVO:
struct AppOrientationUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation: UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
}
}
How to use:
//For portrait
AppOrientationUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo: UIInterfaceOrientation.portrait)
//For landscape
AppOrientationUtility.lockOrientation(UIInterfaceOrientationMask.landscapeRight, andRotateTo: UIInterfaceOrientation.landscapeRight)

Launching an app only in portrait orientation

I'm trying to develop an app on Swift that supports both portrait and landscape orientations but I don't want that the user gets the possibility to launch it in landscape mode.
So is there a way to prevent this opening in landscape while keeping the app's device orientation parameters on portrait, landscape left and landscape right, or is there a way to change the device orientation parameters from portrait only after the launch of the app with the UIInterfaceOrientationMask ?
Thanks a lot for your help!
Restrict it in AppDelegate's method -
var supportedOrientation: UIInterfaceOrientationMask = .portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return supportedOrientation
}
Override following in all view controllers -
override func viewDidAppear(_ animated: Bool) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
var orientation: UIInterfaceOrientationMask = .all
delegate.supportedOrientation = orientation
}
}
Try it and let me know if it works.

Portrait mode all views except one view to be rotated programatically to landscape under a UINavigationController

First of all, my project hierarchy:
tab bar controller -> navigation master -> view controllers
I have one page I want to rotate in landscape mode.
My project is compatible with all the device orientation modes.
I've manually frozen all the view controllers with autorotation returning false:
override func shouldAutorotate() -> Bool {
return false
}
except the one I want to rotate. S
I'm trying this in the view controller I want to be displayed in landscape:
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
But does nothing.
Also, I experimented an issue with this method. When the app was starting in landscape, the view was displaying in landscape and was unable to rotate. I manage to fix it with this method In my app delegate:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if(self.restrictRotation == false){
return UIInterfaceOrientationMask.Portrait//UIInterfaceOrientationMaskPortrait
}
else{
return UIInterfaceOrientationMask.All//UIInterfaceOrientationMaskAll
}
}
And in viewDidLoad setting the var to true where I want to allow rotations:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.restrictRotation = true
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
I've tested with the debugger and the method application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) is called, but the app don't rotate. I guess is because with a tab bar and a navigation controller, things has to be done differently, but don't know how exactly. Can anyone help with this?

Disable Landscape for Split View Controller for iPhone in Universal App

I have a universal app with a UISplitViewController. I want the iPad version to work in both portrait and landscape but I only want the app to work in portrait on iPhone. I tried using the following on my Main View Controller and it did not work:
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
I think the problem is that the split view controller overrides the these two methods. Any ideas?
i think this is what you looking for
[splitViewController setHidesMasterViewInPortrait:NO];
I managed to figure it out by setting the supported app interface orientations at launch in the AppDelegate:
let deviceIdiom = UIScreen.mainScreen().traitCollection.userInterfaceIdiom
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if deviceIdiom == .Pad {
return UIInterfaceOrientationMask.All
}
return UIInterfaceOrientationMask.Portrait
}

Lock screen Orientation to portrait - ios swift

I am creating an application with support only for portrait mode. I don't want Landscape or Portrait upside down. I tried some code. Which allows me to lock in portrait mode.
I am using navigationcontroller and presentviewcontroller. Now my problem is:
1. If I rotate my device upside down and open my application it opens in upside down mode which is wrong.
2. I click some button and enter to presentviewcontroller it returns to portrait mode.
I want all the navigationcontroller and presentviewcontroller in portrait mode
My codes:
I set device orientarion portrait in Target -> General -> Deployment Info -> Portrait
In my appdelagate.swift:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
In my First view controller. Which is basically child of Navigation Controller
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.Portrait ]
}
Edit 1:
Also I set StoryBoard -> ViewController -> Attribute Inspector -> Orientation -> Portrait
Edit 2:
My settings file
Go To Target --> General and set Orientation Mode to Portrait.
Also check info.plist. Make sure Supported Interface Orientations contains only one value(Portrait). Sometime it removes from settings but not updated in plist file.
If your app is not supporting split screen view than you should check the option to Require full screen. This fixed my issue with portrait mode.
If this does not resolve issue add Supported interface orientations Key of Array type in your info.plist. And String item to that array with value Portrait (bottom home button)
For me the only working solution was to add:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
.portrait
}
in the app's delegate class, as indicated in the question itself.
I'm using SwiftUI without any storyboard file, even for the splashscreen.

Resources