2012-03-20 93 views
1

我想在iOS设备旋转到横向时显示不同的全屏视图,并在设备旋转回风景时返回到之前的视图。我主要通过使用一个视图控制器和两个视图来获得它的工作,然后在 - shouldAutorotateToInterfaceOrientation中将视图控制器的self.view设置为适当的视图。如何在iPhone旋转时推送全屏视图控制器?

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
    (interfaceOrientation == UIInterfaceOrientationLandscapeRight))){ 

     self.view = landscapeView; 

    }else if(((interfaceOrientation == UIInterfaceOrientationPortrait) || 
      (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){ 

     self.view = portraintView; 

    } 
    return YES; 
} 

但是,理想情况下,我希望景观视图具有它自己的单独视图控制器来管理视图。我试着推模态的视图控制器和shouldAutorotateToInterfaceOrientation驳回:,但横向视图控制器不上来的正确方向(但仍认为该设备处于纵向)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
    (interfaceOrientation == UIInterfaceOrientationLandscapeRight))){ 

     [self presentModalViewController:landscapeViewController animated:YES]; 

    }else if(((interfaceOrientation == UIInterfaceOrientationPortrait) || 
      (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){ 

     [self dismissModalViewControllerAnimated:YES]; 

    } 
    return YES; 
} 

然后当我回到纵向视图时,它仍然认为设备处于横向状态。

回答

1

你应该在willAnimateRotationToInterfaceOrientation: duration:didRotateToInterfaceOrientation:而不是shouldRotateToInterfaceOrientation做你的轮换工作。然后使用提供的interfaceOrientation来切换您的观点。这种方式更加可靠,只有在您实际旋转设备时才会被调用。

+0

其实我已经忘记了,我试图做的工作在willAnimateRotationToInterfaceOrientation:但我需要做的didRotateToInterfaceOrientation:这样,当我推动视图控制器它将在正确的方向。谢谢! – Austin 2012-03-20 15:41:03

0

正如@MishieMoo所指出的,我需要在didRotateToInterfaceOrientation中完成我的工作,以便视图控制器能够以正确的方向呈现。

所以现在我的纵向视图控制器的代码如下所示:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{  
    return YES; 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    if(fromInterfaceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationPortraitUpsideDown == UIInterfaceOrientationLandscapeRight){ 
     [self performSegueWithIdentifier:@"fullscreenSegue" sender:self]; 
    } 
} 

我做了故事板SEGUE推全屏视图控制器,但是你可以很容易地加载视图控制器,做[自我presentModalViewController:landscapeViewController动画:是]。

和代码以关闭在全屏视图控制器的观点:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{  
    if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight){ 
     [self dismissModalViewControllerAnimated:NO]; 
    } 
} 
相关问题