iOS7 iPad Landscape only app, using UIImagePickerController

你离开我真会死。 提交于 2019-11-27 03:23:25

If your iPad app is landscape only in all conditions, just do these 3 steps :

1) In your app delegate

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskAll;
}

2) Create a category header

#import "UIViewController+OrientationFix.h"

@implementation UIViewController (OrientationFix)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

@end

3) Create a category implementation

#import "UIImagePickerController+OrientationFix.h"

@implementation UIImagePickerController (OrientationFix)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

@end

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

Chintan Prajapati

I have seen this code from Apple's Sample Code.

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;

Due to this UIModalPresentationCurrentContext UIImagePickerController will be opened as per device's current orientation.

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

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

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskAll;
}

UIImagePickerController category

@implement UIImagePickerController (extensions)
- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [[UIApplication sharedApplication] statusBarOrientation];
}

@end

UIViewController category

@implementation UIViewController (extensions)

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

@end

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:

let picker = UIImagePickerController()
picker.modalPresentationStyle = .formSheet
picker.sourceType = .photoLibrary
picker.delegate = self
self.present(picker, animated: true)

Swift 4 solution

Make 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...

import UIKit

class MainNavigationViewController: UINavigationController {

    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscape
    }
}

And of course the code mentioned above for the AppDelegate:

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return .all
}

Btw this is only necessary for iOS 10 and below. Apple seems to have fixed it from iOS 11 and above...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!