2011-08-19 42 views
2

我有一個UIViewController實例(viewController1),我想鎖定在橫向模式下,我能夠做到。在按下按鈕時,我推送另一個支持兩個方向的UIViewController(viewController2)。現在,如果用戶將viewController2的方向更改爲縱向並返回viewController1,則viewController1也會將其方向更改爲縱向。我怎樣才能避免這種情況?UIViewController方向

在此先感謝。

回答

0

添加這些方法的視圖控制器

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    回報(interfaceOrientation == UIInterfaceOrientationPortrait); }

那對第一

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    返回是); }

那第二

- (BOOL)shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)toInterfaceOrientation { 如果(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){ 返回YES; } return NO; }

+0

似乎沒有工作。當我從縱向模式下的第二個視圖控制器返回時,第一個視圖控制器仍然進入縱向模式。順便說一句我想鎖定第一個視圖控制器在橫向模式。 –

0

如果這兩個控制器都是UIViewController的實現,兩個不同的類彼此!你可以在第一個實現方法shouldAutorotateToInterfaceOrientation!這應該工作,即使你回去!

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) { 
    return YES; 
} 
return NO;} 
+0

似乎不起作用。當我從縱向模式下的第二個視圖控制器返回時,第一個視圖控制器仍然進入縱向模式。順便說一句我想鎖定第一個視圖控制器在橫向模式。 –

0

你說推,所以我假設兩個ViewControllers都在一個NavigationController。如果是這樣,我將不得不讓你失望,你想要的是不可能的。你的循環回調工作正常,他們響應旋轉,你不能強制它。發生的是正確的行爲。

最好的解決方案是防止用戶回到以前的ViewController不支持的方向,例如隱藏後退按鈕。

一段時間後,我做了自己的NavigationController(不從UIViewController繼承,但它可以做同樣的事情),我試圖實現你正在嘗試做的事情。在推送或彈出之前,如果即將顯示的ViewController視圖不支持當前方向,我將該視圖控制器的視圖轉換90°,並強制將狀態欄的方向改爲新的ViewController支持的方向。 只要推或彈完成,我會做一個小技巧強制設備的旋轉。如果從窗口中刪除rootViewController的視圖並重新添加它,響應者鏈將被強制經歷所有循環回調。當發生這種情況時,我檢查了ViewController的視圖是否已轉換並重置了該轉換。

它確實工作,主要是。但是這是很亂的代碼,它違背了Apple的當前策略,即rootViewController應負責處理方向。另外在iOS6中強制狀態欄的方向保證有效。所以我真的建議不要這樣做,我也從我自己的NavigationController中刪除了這個。

相關問題