iOS旋转仅查看一个视图控制器的视图

时间:2012-08-18 18:28:32

标签: iphone objective-c ios autorotate auto-rotation

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

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

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

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

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

enter image description here

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

2 个答案:

答案 0 :(得分:3)

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

在iOS 6中被折旧,所以如果这就是你的项目运行的原因,那就是它无法正常运行的原因。你需要做的是实施:

- (NSUInteger)supportedInterfaceOrientations
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

第一个将告诉控制器允许使用哪个方向,第二个将告诉它首先使用哪个方向。请注意,只有在方法shouldAutorotate:return YES。

时才调用第一种方法

这些是您可以用于supportedInterfaceOrientations的常量:

UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskPortraitUpsideDown
UIInterfaceOrientationMaskLandscape
UIInterfaceOrientationMaskAll
UIInterfaceOrientationMaskAllButUpsideDown

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

答案 1 :(得分: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;
}