我认为这是一个常见的问题,许多答案不再适用,很多只是部分答案,如果你在iOS7下,你的iPad应用程序只是横向,但你想使用UIImagePickerController
来源{ {1}}或UIImagePickerControllerSourceTypePhotoLibrary
。
如何设置正确,所以它100%正常工作?并且您没有获得混合方向并避免错误“支持的方向与应用程序没有共同的方向,UIImagePickerControllerSourceTypeCamera
返回shouldAutorotate
”。
答案 0 :(得分:83)
如果您的iPad应用仅在所有条件下均为横向,请执行以下3个步骤:
1)在您的app delegate
中- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskAll;
}
2)创建类别标题
#import "UIViewController+OrientationFix.h"
@implementation UIViewController (OrientationFix)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
@end
3)创建类别实施
#import "UIImagePickerController+OrientationFix.h"
@implementation UIImagePickerController (OrientationFix)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
@end
注意:您无需在任何地方导入这些类别,只需使用项目
进行编译即可注意:无需在任何VC中实现这些方法
注意:无需更改plist支持的方向
经过测试,可在任何条件下使用
答案 1 :(得分:41)
我在Apple的示例代码中看到了这段代码。
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
由于此 UIModalPresentationCurrentContext UIImagePickerController
将根据设备的当前方向打开。
答案 2 :(得分:1)
Apple的文档说:
“重要:UIImagePickerController类仅支持纵向模式。”
尽管如此,它可以在全景和iOS 6的横向上正常工作。
答案 3 :(得分:0)
感谢Peter Lapisu的建议。它对我不起作用(也许我在iOS 8.3上),但我能够修改它以修复我的方向问题。我的代码在
之下在app delegate中
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskAll;
}
UIImagePickerController类别
@implement UIImagePickerController (extensions)
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [[UIApplication sharedApplication] statusBarOrientation];
}
@end
UIViewController类别
@implementation UIViewController (extensions)
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
@end
答案 4 :(得分:0)
虽然大多数答案建议使用.currentContext
,但我在解雇图片选择器后发现,一切都错了。
在Landtscaped iPad上, imho 如果您使用.formSheet
, imho 最好:
let picker = UIImagePickerController()
picker.modalPresentationStyle = .formSheet
picker.sourceType = .photoLibrary
picker.delegate = self
self.present(picker, animated: true)
答案 5 :(得分:0)
Swift 4
解决方案
请确保在将ViewController嵌入到NavigationViewController中时在此处进行修复。然后,将这种限制添加到ViewController不会起作用。
import UIKit
class MainNavigationViewController: UINavigationController {
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscape
}
}
当然还有上面提到的AppDelegate的代码:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return .all
}
顺便说一句,这仅适用于iOS 10及以下版本。苹果似乎已经从iOS 11及更高版本对其进行了修复...