4

我在一个项目中有两个视图控制器。但是,我希望其中一个视图控制器自动旋转,而另一个不自动旋转。

如果我设置主项目设置,如下所示: 在此处输入图像描述

然后,所有视图控制器都会自动旋转,无论视图控制器中的以下代码如何,我都不想自动旋转:

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

但是,如果我如下所示设置主项目设置,我不想自动旋转的视图控制器不会,但这也意味着我想要的视图控制器也不能。

在此处输入图像描述

我必须如何将主项目(plist 文件)设置与视图控制器的设置集成,以便一个视图控制器自动旋转而另一个视图控制器不会?

4

2 回答 2

3
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

在 iOS 6 中已贬值,因此如果您的项目正在运行,这就是它无法正常工作的原因。您需要做的是实施:

- (NSUInteger)supportedInterfaceOrientations
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

第一个将告诉控制器允许使用哪个方向,第二个将告诉它首先使用哪个方向。请注意,仅当方法 shouldAutorotate: 返回 YES 时才调用第一个方法。

这些是可用于supportedInterfaceOrientations 的常量:

UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskPortraitUpsideDown
UIInterfaceOrientationMaskLandscape
UIInterfaceOrientationMaskAll
UIInterfaceOrientationMaskAllButUpsideDown

请注意,这些仅适用于 iOS 6.0。

于 2012-12-05T19:04:04.317 回答
0

假设我正在使用 tabbarController & iOS<6.0 尝试使用以下代码解决您的问题:

//In First View Controller

//BOOL activeStatus;

-(void)viewWillAppear:(BOOL)animated
{
 activeStatus=YES;
}


-(void)viewWillDisappear:(BOOL)animated
{
 activeStatus=NO;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) && activeStatus==YES)
{
    return YES;
}

return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

//In Second View Controller

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

 return YES;
}
于 2013-03-14T07:04:19.880 回答