2012-03-20 117 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]; 
    } 
}