您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 6 開發:UINavigationController 的橫豎屏轉換

編輯:iOS 開發綜合

在 iOS 6 中,若 App 的根控制器是 UINavigationController,要實作特定頁面橫向或縱向顯示,需要讓 navigation controller 把方向判斷轉交給目前顯示的 view controller。

AppDelegate 設定

首先在 AppDelegate 中允許所有方向,由個別頁面自行決定。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskAll;
}

NavigationController 轉發

可為 UINavigationController 建立 category,將方向相關方法轉發給 topViewController。

- (BOOL)shouldAutorotate {
    return [[self topViewController] shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations {
    return [[self topViewController] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [[self topViewController] preferredInterfaceOrientationForPresentation];
}

個別頁面指定方向

在需要橫向的頁面中,可回傳橫向遮罩;若需要縱向,則改為縱向。

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight;
}

- (BOOL)shouldAutorotate {
    return YES;
}

重要提醒

頁面切換時,若需要讓方向設定正確生效,建議使用 presentViewController 呈現新的 navigation controller,而不是單純用 push 切換。

[self presentViewController:navigationController animated:YES completion:nil];
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved