iOS旋转视图:仅旋转一个视图控制器的视图。

4

我在一个项目中有两个视图控制器。然而,我想让其中一个视图控制器自动旋转,而另一个不旋转。

如果我设置主项目设置如下: enter image description here

那么,所有视图控制器都会自动旋转,无论以下代码是否存在于我不想自动旋转的视图控制器中:

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

然而,如果我像下面所示设置主项目设置,那么我不想自动旋转的视图控制器就不会旋转,但这也意味着我想要旋转的视图控制器也不能旋转。
如何将主项目(plist文件)设置与视图控制器的设置相结合,以便一个视图控制器可以自动旋转,而另一个则不行?

2
请查看这个答案 - manikal
你的两个 ViewController 之间的关系怎么样?例如,是 Push 关系还是 Modal 关系? - bitmapdata.com
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。

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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接