在 iOS 中,若要讓 App 整體支援多方向,但一般頁面維持豎屏,可以透過工程設定、AppDelegate 與視圖控制器的 supportedInterfaceOrientations 一起控制。
可在 AppDelegate 實作 application:supportedInterfaceOrientationsForWindow:,讓系統知道整個 App 支援哪些方向。
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return UIInterfaceOrientationMaskAll;
}
若使用 UINavigationController 或自訂 BaseViewController,可讓 shouldAutorotate 回傳 YES,但 supportedInterfaceOrientations 只回傳 Portrait。這樣一般頁面會保持豎屏,需要橫屏的頁面再以 present 顯示。
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
播放器若需要支援橫屏,可在該控制器回傳 Landscape 或 All。若同時支援橫豎屏,則回傳 UIInterfaceOrientationMaskAll。
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
此外,若需要控制狀態列顯示或隱藏,可實作 prefersStatusBarHidden,並呼叫 setNeedsStatusBarAppearanceUpdate 更新外觀。