5

我想從支持景觀(而在景觀模式)的視圖控制器,到一個明確沒有(也不應該)支持景觀。我這樣做如下:問題pushViewController從景觀到肖像

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait); 
} 

理想情況下,我想新的viewController,我推入到堆棧中最初開始在肖像,而不是風景。奇怪的是,即使這個方法實施,它開始在景觀。

我唯一的猜測是,蘋果不希望用戶從風景轉換爲肖像(儘管允許我們從風景轉回到先前的肖像控制器)。

任何見解和/或幫助將不勝感激。

回答

10

我找到了強制肖像的方法。這有點破解,但現在就是這樣。在 - (空)的的viewController的要強制肖像用於執行以下操作viewDidLoad中:

UIViewController *viewController = [[UIViewController alloc] init]; 
[self presentModalViewController:viewController animated:NO]; 
[self dismissModalViewControllerAnimated:NO]; 
[viewController release]; 

這基本上迫使畫像,通過提供一個控制器(僅默認支持人像)。

+0

這對我來說只是通過pushViewController從縱向移動到僅橫向的視圖。保存你不得不調用未公開的私人API [[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeRight]謝謝! – MyCSharpCorner 2011-10-29 07:26:40

4

您需要以模態方式呈現您的新視圖控制器。如果您的視圖控制器存在於導航控制器中,那麼導航堆棧中所有視圖控制器的方向由堆棧中的根視圖控制器隱含。無論您在nav stack中從根視圖控制器返回的shouldAutoRotateToInterfaceOrientation是否適用於它下面的所有視圖控制器。

+0

啊我明白了,這就是我所假設的情況。感謝您的澄清! – Sahil 2011-01-28 18:37:50

1

以上Sahil的回答自iOS 6.0起棄用。然而,下面似乎也是這樣做的:

UIViewController *viewController = [[UIViewController alloc] init]; 
[self presentViewController:viewController animated:NO completion:nil]; 
[self dismissViewControllerAnimated:NO completion:nil]; 
[viewController release];