1

我有一个 UIImagePickerController,我希望它的方向始终保持纵向模式。应用程序本身设置为纵向模式,但 UIImagePickerController 改变了方向。我使用以下代码创建了一个自定义 UIImagePickerController:

#import "ViewControllerImage.h"

@interface ViewControllerImage ()
- (BOOL)shouldAutorotate;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
- (NSUInteger)supportedInterfaceOrientations;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
@end

@implementation ViewControllerImage

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (NSUInteger)supportedInterfaceOrientations {

return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {

return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

我以这种方式呈现它:

ViewControllerImage *picker = [[ViewControllerImage alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:picker animated:YES completion:nil];

代码不起作用控制器仍然旋转。为什么?

4

1 回答 1

1

作为最简单的解决方案 - 在您的 proj 文件中允许纵向模式。

作为第二种解决方案 - 尝试将以下代码添加到您的应用程序委托中:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationPortrait;
}
于 2013-09-21T11:42:20.410 回答