xcode 在 UIImagePickerController 中强制横向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14618546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Force landscape orientation in UIImagePickerController
提问by Fernando
I'm new to iOS development, and Im developing my first app. (so sorry if Im asking a newbie question)
我是 iOS 开发的新手,我正在开发我的第一个应用程序。(很抱歉,如果我问一个新手问题)
My app is all in portrait orientation, except to the place where I'm using UIImagePickerControllerto take a picture to be used in the app, what I'm doing is:
我的应用程序都是纵向的,除了我使用UIImagePickerController拍摄要在应用程序中使用的照片的地方,我正在做的是:
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 UIImagePickerControllerbe in landscape only. if I try to use:
由于我的应用程序在Portrait 中,我需要为UIImagePickerController制作一些东西,只能在横向中使用。如果我尝试使用:
[[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.1I will need to implement something for iOS 6.0 too, is it correct?
似乎我不能使用,因为我的 Xcode 在这行中识别为错误(可能是因为 Xcode 版本,我不确定)。我不确定,但似乎如果我正在为iOS 5.1开发,我也需要为 iOS 6.0实现一些东西,对吗?
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?
有人可以帮助我了解制作这个UIImagePickerController需要什么,并且只有应用程序中的这个视图控制器可以横向工作并在 iOS 5.1 和 6 中工作吗?
Regards
问候
回答by Glenn Barnett
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 cameraOverlayViewproperty and use that view to present additional information or manage the interactions between the camera interface and your code.
重要提示:
UIImagePickerController
该类仅支持纵向模式。此类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改,只有一个例外。您可以将自定义视图分配给cameraOverlayView属性,并使用该视图显示附加信息或管理相机界面和代码之间的交互。
So it looks like forcing landscape mode is unsupported and discouraged, unless you do so by replacing the default controls with a custom cameraOverlayView
.
因此,似乎不支持也不鼓励强制横向模式,除非您通过将默认控件替换为自定义cameraOverlayView
.
回答by Asif Mujteba
Create a class named "CUIImagePickerController" inherited from UIImagePickerController, override following methods, now use this class!
创建一个继承自 UIImagePickerController 的名为“CUIImagePickerController”的类,覆盖以下方法,现在使用这个类!
@interface CUIImagePickerController : UIImagePickerController
@end
@implementation CUIImagePickerController
- (BOOL)shouldAutorotate {
return NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient {
return (orient == UIInterfaceOrientationLandscapeLeft) | (orient == UIInterfaceOrientationLandscapeRight);
}
@end
Regards,
问候,
回答by Ravindra Bagale
try implementing below methods:
尝试实现以下方法:
- (BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}