2014-10-30 36 views
1
-(BOOL)shouldAutorotate { 

    return NO; 

} 

上面的方法適用於一個控制器,但是當有多個viewController在堆棧上推送時。
我想要一個特定的控制器,只能以縱向模式顯示。如何在iOS 8中禁用特定控制器的自動旋轉?

- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { 

} 

我已經使用上面的方法建議在iOS 8的堆棧溢出,但它沒有給出預期的結果。

回答

8

首先,使用-supportedInterfaceOrientations而不是-shouldAutorotate-shouldAutorotate只能在必須根據運行時確定的因素禁止自動旋轉時使用。你知道你的視圖控制器總是隻支持縱向模式,這裏沒有運行時決定。

接下來,導航控制器的代理必須實現-navigationControllerSupportedInterfaceOrientations:方法,以在導航堆棧頂部的視圖控制器上返回調用-supportedInterfaceOrientations的結果。

-(NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController { 
    return navigationController.topViewController.supportedInterfaceOrientations; 
} 

一個重要的警告:推到導航堆棧上的視圖控制器無法控制其初始界面方向;這將始終是當前的界面方向。上述技術將做的是防止界面在顯示視圖控制器時旋轉到除肖像以外的任何方向。

+0

男人,我解決你..你解決了我的問題.... – 2015-09-08 12:23:26

+0

這是做到這一點的正確方法。 但在我的情況下,我也必須重寫NavigationController的[UIViewController shouldAutorotate](繼承它)以返回navigationController.topViewController.shouldAutorotate。 – befstrat 2015-09-15 08:55:00

+0

另外,不要忘記檢查topViewController是否爲空 – henon 2015-10-05 15:13:00

相關問題