Force landscape orientation in UIImagePickerController

我们两清 提交于 2019-11-26 07:45:45

问题


I\'m new to iOS development, and Im developing my first app. (so sorry if Im asking a newbie question)

My app is all in portrait orientation, except to the place where I\'m using UIImagePickerController to take a picture to be used in the app, what I\'m doing is:

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
picker.allowsEditing = NO;
[self presentModalViewController:picker animated:YES];

As my app is in Portrait, I need to make something to the UIImagePickerController be in landscape only. if I try to use:

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

It seems that I can\'t use, because my Xcode identify as a error in this lines (probably because of Xcode the version, I\'m not sure). And I\'m not sure, but it seems that if I\'m developing for iOS 5.1 I will need to implement something for iOS 6.0 too, is it correct?

Can someone help me to understand what is need to make this UIImagePickerController, and only this view controller in the app, work in Landscape orientation and works in iOS 5.1 and 6?

Regards


回答1:


As per the UIImagePickerController Class Reference

Important: The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified, with one exception. You can assign a custom view to the cameraOverlayView property and use that view to present additional information or manage the interactions between the camera interface and your code.

So it looks like forcing landscape mode is unsupported and discouraged, unless you do so by replacing the default controls with a custom cameraOverlayView.




回答2:


Create a class named "CUIImagePickerController" inherited from UIImagePickerController, override following methods, now use this class!

@interface CUIImagePickerController : UIImagePickerController

@end

@implementation CUIImagePickerController
- (BOOL)shouldAutorotate {
    return NO;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient {
    return (orient == UIInterfaceOrientationLandscapeLeft) | (orient == UIInterfaceOrientationLandscapeRight);
}
@end

Regards,




回答3:


try implementing below methods:

- (BOOL)shouldAutorotate
{

return YES;
}

-(NSUInteger)supportedInterfaceOrientations

{
return UIInterfaceOrientationMaskLandscape;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  return UIInterfaceOrientationLandscapeRight;
}


来源:https://stackoverflow.com/questions/14618546/force-landscape-orientation-in-uiimagepickercontroller

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