我在装有iOS 6.0的iPad 4上工作。
我有一个具有以下init的ViewController(MyPickerController):
- (id)init
{
self = [super init];
if (self) {
_picker = [[UIImagePickerController alloc] init];
_picker.delegate = self;
_picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self.view addSubview:_picker.view];
}
return self;
}
我实现了以下UIPickerControllerDelegate方法来弃用MyPickerController:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
好吧,我有另一个 View Controller 以
FormSheetStyle
模态显示,当我点击一个按钮时,我想用以下代码显示MyPickerController
:MyPickerController * pickerVC = [[MyPickerController alloc] init];
[self presentViewController:pickerVC animated:YES completion:nil];
在我的AppDelegate中,我具有以下加法方法:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return (NSUInteger)[application supportedInterfaceOrientationsForWindow:window] | (1<<UIInterfaceOrientationPortrait);
}
当我点击
UIIMagePicker
的取消按钮进入MyPickerController
时,应用程序崩溃并出现以下错误:Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation must return a supported interface orientation!
阅读有关stackoverflow的相关问题,我还创建了以下UIImagePickerController类别:
@implementation UIImagePickerController (NonRotating)
- (BOOL)shouldAutorotate
{
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationMaskPortrait;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
@end
谢谢!
最佳答案
尝试这样做。
如果您的 View Controller 位于UINavigationController内,则应为Navigationcontroller使用以下类别:
@implementation UINavigationController (autorotate)
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationMaskPortrait;
}
@end