How to prevent a modal UIImagePickerController from rotating?

删除回忆录丶 提交于 2019-12-09 05:30:38

问题


I have an app that fully support rotation. I am modally adding a UIImagePickerController for which there is no support for UIInterfaceOrientationLandscape and I cannot get the controller to stay in portrait.

In other words, I need to disable rotation for the UIImagePickerController so it stays in portrait, without removing rotation for the rest of my app. this seems basic, but I can't seem to locate it. How can I prevent this rotation?



UPDATE

As suggested, I tried subclassing with the following code:

@interface UAImagePickerController : UIImagePickerController {
}
@end

@implementation UAImagePickerController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIDeviceOrientationIsPortrait(toInterfaceOrientation);
}
@end

The line is not being hit at all with a breakpoint… i think there must be something funky about the UIImagePickerView


回答1:


Subclass UIImagePickerController and have that new class implement shouldAutorotateToInterfaceOrientation: to make it rotate to portrait only, something like this:

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



回答2:


I created my UIImagePickerController inside a UIViewController with the respective shouldAutorotateToInterfaceOrientation method implemented and presented it like this

[self.view addSubview:myImagePicker.view];
[targetVC.navigationController presentModalViewController:self animated:YES];

Hope that helps.




回答3:


You should override _isSupportedInterfaceOrientation:, and not the shouldAutorotateToInterfaceOrientation:

iPad 2 UIImagePickerController camera auto-rotation driving me mad!




回答4:


Just drop this in above your ViewControllers @implementation block (in the .m file)

@implementation UIImagePickerController (portrait)

- (BOOL)_isSupportedInterfaceOrientation:(UIDeviceOrientation)orientation
{
    return UIDeviceOrientationIsPortrait(orientation);
}

@end


来源:https://stackoverflow.com/questions/4357045/how-to-prevent-a-modal-uiimagepickercontroller-from-rotating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!