[英]UIImagePickerController to pick from Album in landscape only iPhone/iPad app
[英]iOS7 iPad Landscape only app, using UIImagePickerController
我相信这是一个常见的问题,如果您在iOS7下并且iPad应用程序仅是“横向”,那么许多答案将不再起作用,许多答案只是部分解决,但是您想将UIImagePickerController
与源UIImagePickerControllerSourceTypePhotoLibrary
或UIImagePickerControllerSourceTypeCamera
。
如何正确设置,使其100%工作? 而且您不会混淆方向,并且避免出现错误“支持的方向与应用程序没有共同的方向,并且应该shouldAutorotate
返回YES
”。
如果您的iPad应用仅在所有情况下都是横向的,则只需执行以下3个步骤:
1)在您的应用程序委托中
- (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支持的方向
经过测试,可以在任何条件下工作
我已经从Apple的示例代码中看到了此代码。
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
由于这个UIModalPresentationCurrentContext UIImagePickerController
将根据设备的当前方向打开。
感谢上面的Peter Lapisu建议。 它对我不起作用(也许我在iOS 8.3上),但是我能够对其进行修改以解决我的方向问题。 我的代码如下
应用内委托
- (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
尽管大多数答案建议使用.currentContext
,但我在关闭图像选择器后才发现,一切都错了。
在美化的iPad上, 恕我直言 ,最好使用.formSheet
:
let picker = UIImagePickerController()
picker.modalPresentationStyle = .formSheet
picker.sourceType = .photoLibrary
picker.delegate = self
self.present(picker, animated: true)
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及更高版本修复了该问题...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.