I believe this is a common issue and many answers don't work anymore, many just partial, if you are under iOS7 and your iPad app is Landscape only, but you want to use the UIImagePickerController
with source UIImagePickerControllerSourceTypePhotoLibrary
or UIImagePickerControllerSourceTypeCamera
.
How to set it right, so it's working 100%? And you don't get mixed orientations and avoid the error "Supported orientations has no common orientation with the application, and shouldAutorotate
returns YES
".
Apple's documentation says:
"Important: The UIImagePickerController class supports portrait mode only."
Although, it works fine in landscape for full screen and on iOS 6.
UIImagePickerController class reference
Swift 4
solutionMake sure that when you are having the ViewController embedded in a NavigationViewController to have the fix there. It won't work adding this restriction to the ViewController then...
And of course the code mentioned above for the AppDelegate:
Btw this is only necessary for iOS 10 and below. Apple seems to have fixed it from iOS 11 and above...
If your iPad app is landscape only in all conditions, just do these 3 steps :
1) In your app delegate
2) Create a category header
3) Create a category implementation
Note: You don't need to import these categories anywhere, just enough they are compiled with the project
Note: no need to implement these methods in any VC
Note: no need to change your plist supported orientations
This is tested and working under any conditions
Although most answers recommend using
.currentContext
, I have found out after dismissing the imagepicker, everything was wrong.On an Landscaped iPad, imho it's best if you would use
.formSheet
:Thanks to Peter Lapisu suggestion above. It doesn't work for me (maybe i'm on iOS 8.3) but i was able to modified it to fixed my orientation issues. My codes are below
In app delegate
UIImagePickerController category
UIViewController category
I have seen this code from Apple's Sample Code.
Due to this UIModalPresentationCurrentContext
UIImagePickerController
will be opened as per device's current orientation.