我正在创建一个横屏模式的应用程序,并使用UIImagePickerController
在其中使用iPhone相机拍照,我也希望将其创建为横屏模式。
但是,正如苹果文档所建议的那样,UIImagePickerController
不支持横向方向,那么我该怎么做才能获得所需的功能?
我正在创建一个横屏模式的应用程序,并使用UIImagePickerController
在其中使用iPhone相机拍照,我也希望将其创建为横屏模式。
但是,正如苹果文档所建议的那样,UIImagePickerController
不支持横向方向,那么我该怎么做才能获得所需的功能?
如果您想在横向模式下使用UIImagePickerController,请使用user1673099的答案,但是不要使用:
- (BOOL)shouldAutorotate
{
return NO;
}
使用:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
然后选择器会以横屏模式打开:
但请确保在部署信息中勾选“纵向”:
UIImagePickerController
只支持竖屏模式。如果你不在全局范围内支持竖屏,图像选择器将会崩溃,因为它没有可用的方向。 - pkamb...而且我也希望在横向模式下创建它。
一行代码可以产生很大的影响!在您的IBAction所在的方法或函数中:
在Swift中,
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
// .overCurrentContext allows for landscape and portrait mode
imagePickerController.modalPresentationStyle = .overCurrentContext
Objective-C,
->Objective-C,
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
[imagePickerController setDelegate:self];
[imagePickerController setModalPresentationStyle: UIModalPresentationOverCurrentContext];
注意:这将允许imagePickerController正确地呈现其视图,但可能不能在呈现时修复旋转问题。
尝试这种方法...
根据苹果文档,图像选择器控制器在横向模式下永远不会旋转。您只能在纵向模式下使用。
要仅为ImagePicker Controller禁用横向模式,请按以下代码操作:
在您的ViewController.m中:
将子类(NonRotatingUIImagePickerController)设为图像选择器控制器
@interface NonRotatingUIImagePickerController : UIImagePickerController
@end
@implementation NonRotatingUIImagePickerController
// Disable Landscape mode.
- (BOOL)shouldAutorotate
{
return NO;
}
@end
使用方法如下
UIImagePickerController* picker = [[NonRotatingUIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
etc.... Just as Default ImagePicker Controller
这对我有效,如果你有任何问题,请告诉我。
在 iOS 10/11 中,与 Swift 4.0 配合使用效果非常好。
import UIKit
extension UIImagePickerController {
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .all
}
}
只需将扩展名放在项目的某个位置,无需为其子类化即可使用。
如果需要指定设备类型,可以添加以下检查:
import UIKit
extension UIImagePickerController {
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIDevice.current.userInterfaceIdiom == .phone ? .portrait : .all
}
}
这将允许iPad自由旋转,但在手机上强制使用纵向模式。只需确保您的应用程序在其info.plist中配置以支持这些功能,否则启动选择器时可能会遇到崩溃。
这是一个支持在所有界面方向旋转的版本:
/// Not fully supported by Apple, but works as of iOS 11.
class RotatableUIImagePickerController: UIImagePickerController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .all
}
}
这样,如果用户旋转设备,它将更新选择器控制器以支持当前方向。只需像通常实例化UIImagePickerController一样即可。
如果您只想支持子集方向,则可以返回不同的值。
在不使用任何hack的情况下,在横向模式下正确使用UIImagePickerController
的方法是将其放入UIPopoverController
中。
- (void)showPicker:(id)sender
{
UIButton *button = (UIButton *)sender;
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
_popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[_popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
修改上述代码的方法
- (NSUInteger)supportedInterfaceOrientations
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIDeviceOrientationLandscapeRight || orientation == UIDeviceOrientationLandscapeLeft)
return UIInterfaceOrientationMaskLandscape;
else
return UIInterfaceOrientationMaskPortrait;
}
已经接受的答案对我不起作用。 我还必须将modalPresentationStyle添加到UIImagePickerController中,使其正常工作。
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
pickerController.modalPresentationStyle = UIModalPresentationCurrentContext; //this will allow the picker to be presented in landscape
pickerController.delegate = self;
pickerController.allowsEditing = YES;
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:pickerController animated:YES completion:nil];
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape; //this will force landscape
}
但是根据苹果的文档,不支持在横屏模式下显示此选择器,因此请注意。
UIImagePickerController
的视图控制器应该具有 supportedInterfaceOrientations
。在我的示例中,委托是呈现选择器的控制器。我会更新我的答案。 - Michal Cichon