Reputation: 13123
My app is in full landscape mode but when picking an image from the photo album I am forced to add portrait mode. I just want portrait mode to work when selecting an image. Now it is acting weird, when having no direction lock and selecting an image while having the phone in landscape mode, the image picker is shown but in half. The right half is black and the left is the image picker in landscape (so yeah, that looks weird). This are my settings:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return .all
}
In my ViewController in viewDidLoad( I need to add this in every ViewController... ):
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
private func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.landscapeLeft
}
open override var shouldAutorotate: Bool {
get {
return false
}
}
I already looked through the answers of people questioning this. I just can not get it to work :(
Upvotes: 0
Views: 1044
Reputation: 547
I had a similar problem a while back. If I understand you question correctly, you want the app to stay in landscape mode unless the photo picker is presented. Here is what you have to do:
Go into your AppDelegate and add this static variable:
static var canRotate = false {
didSet{
UIDevice.current.setValue(canRotate ? UIInterfaceOrientation.portrait.rawValue : UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
}
}
Also in your AppDelegate, add this function:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if AppDelegate.canRotate {
return .portrait
} else {
//return the orientation you want your app to be in
return [.landscapeLeft, .landscapeRight]
}
}
At this point, you can control the whether or not you can rotate the device by calling AppDelegate.canRotate = true or false
All that is left is to add this variable in your image picker code, here's what you do:
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = true
AppDelegate.canRotate = true
self.present(imagePicker, animated: true, completion: nil)
}
Also, in this function:
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
AppDelegate.canRotate = false
picker.dismiss(animated: true, completion: nil)
}
And in this one as well:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
AppDelegate.canRotate = false
picker.dismiss(animated: true, completion: nil)
}
There may be optimizations for this code but this is the core functionality you'll need.
Let me know how this goes for you, if it works, glitches, or what not.
Upvotes: 2