iPad:仅将UIView方向锁定为横向模式

时间:2011-05-31 14:23:12

标签: ipad

此要求适用于 iPad。 我有一个UIView,在启动视频录制时用作相机叠加视图。它在UIImagePickerController中设置如下。

[self presentModalViewController:pickerController animated:NO];
pickerController.cameraOverlayView =myOwnOverlay;

这是我的要求,我必须在调用摄像机进行视频录制时在UIImagePickerController中提供我自己的叠加层。 我想将我自己的相机覆盖UIView锁定到LANDSCAPE模式,这样允许用户只能以横向模式录制视频,而不能以纵向模式录制,这也是我的项目要求。 我知道用于UIViewController的shouldAutorotateToInterfaceOrientation。我使用“[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];”它,当我启动这个相机自己的叠加UIView时它会锁定到横向模式,但是当我旋转设备时,UIView也会旋转到肖像。我根本不想要肖像模式。我尝试像下面这样处理这个问题,但它不适用于UIView。然后我看到这在UIviewController中是可能的,但在UIView中却没有。但我必须有UIView用于这个相机启动操作。

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

return (interfaceOrientation==UIInterfaceOrientationLandscapeRight || interfaceOrientation==UIInterfaceOrientationLandscapeLeft);
}

请建议我如何为我的UIView提供方向锁的解决方案?

谢谢!

3 个答案:

答案 0 :(得分:1)

怀疑是否允许来自APPLE但是如果你想让UIImagePickerController以横向方向启动(并保持),请使用以下代码。

//Initialize picker

UIImagePickerController * picker = [[UIImagePickerController alloc] init];
   picker.delegate = self;


//set Device to Landscape. This will give you a warning. I ignored it.
//warning: 'UIDevice' may not respond to '-setOrientation:'


[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

//Set Notifications so that when user rotates phone, the orientation is reset to landscape.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

//Refer to the method didRotate:   
[[NSNotificationCenter defaultCenter] addObserver:self
              selector:@selector(didRotate:)
               name:@"UIDeviceOrientationDidChangeNotification" object:nil];

//Set the picker source as the camera   
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

//Bring in the picker view   
[self presentModalViewController:picker animated:YES];

方法didRotate:

- (void) didRotate:(NSNotification *)notification

{
      //Maintain the camera in Landscape orientation
 [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

}

此代码属于UIImagePickerController in Landscape

答案 1 :(得分:1)

试试这个:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

{
    BOOL isLandscapeRight = (UIInterfaceOrientationLandscapeRight == interfaceOrientation);
    return isLandscapeRight;
}

并且还必须将您的应用程序info.plist的界面方向设置为横向(右侧主页按钮)

答案 2 :(得分:1)

有一个简单的解决方案 在info.plist文件中编辑条目支持的界面方向ipad。

enter image description here