今天,做一個(gè)項(xiàng)目,本意是首頁是要屏幕方向是固定的portrait,rootViewController是navigationController,不論開啟App之前是橫屏或豎屏,進(jìn)到App就變成portrait.
要在UINavigationController的子類里加
override var shouldAutorotate: Bool{
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
preferredInterfaceOrientationForPresentation: UIInterfaceOrientation這個(gè)方法在啟動(dòng)時(shí)一直沒有調(diào)用,根據(jù)字面的猜想它只在一個(gè)viewController presentation到下一具viewController時(shí),下一個(gè)viewController會(huì)調(diào)用。
上面這樣設(shè)置會(huì)對(duì)navigationController里所有viewController都有效,如果要對(duì)每個(gè)viewController都有自定義的設(shè)置,請(qǐng)看下面的文章
iOS 橫豎屏切換(控制項(xiàng)目的每一個(gè)ViewController)
最好在func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool里加
UIApplication.shared.statusBarOrientation = .portrait