簡體   English   中英

UIViewController方向

[英]UIViewController orientations

我有一個UIViewController實例(viewController1),我想將其鎖定在橫向模式下。 在點擊按鈕時,我按下另一個支持兩種方向的UIViewController(viewController2)。 現在,如果用戶將viewController2的方向更改為縱向並返回到viewController1,則viewController1還將其方向更改為縱向。 我該如何避免呢?

提前致謝。

您說推送,所以我假設兩個ViewController都在NavigationController中。 如果是這樣,我將使您失望,那么您想要的就不可能了。 您的輪播回調正常運行,它們響應輪播,您無法強制執行。 發生的是正確的行為。

最好的解決方案是當您處於以前的ViewController不支持的方向時,防止用戶返回,例如隱藏“后退”按鈕。

前一陣子,我制作了自己的NavigationController(雖然它不繼承自UIViewController,但它可以做的完全一樣),並且嘗試實現您要執行的操作。 在推送或彈出之前,如果要顯示的ViewController的視圖不支持當前方向,則將該ViewController的視圖轉換了90°,然后將狀態欄的方向強制為新的ViewController支持的方向。 推入或彈出完成后,我將做一個小技巧來強制設備旋轉。 如果從窗口中刪除rootViewController的視圖並重新添加,則將強制響應者鏈通過所有旋轉回調。 發生這種情況時,我檢查了ViewController的視圖是否已轉換並重置了該轉換。

它確實起作用了。 但這是凌亂的代碼,並且違反了Apple當前的政策,即rootViewController應該負責處理方向。 同樣在iOS6中,強制狀態欄方向也可以正常工作。 因此,我建議您不要這樣做,我也已從自己的NavigationController中刪除了它。

將這些方法添加到視圖控制器

  • (BOOL)應該AutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    返回(interfaceOrientation == UIInterfaceOrientationPortrait); }

多數民眾贊成在第一

  • (BOOL)應該AutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    返回是); }

那是第二個

-(BOOL)應該AutorotateToInterfaceOrientation:UIInterfaceOrientation)到InterfaceOrientation {如果(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){返回YES; }返回NO; }

如果這兩個控制器都是UIViewController的實現,則兩者互不相同! 您可以只在第一個中實現方法shouldAutorotateToInterfaceOrientation! 即使您回去,這也應該起作用!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM