使用 UINavigationController 作為根視圖控制器,但不想顯示頂部導航列。
navigationController.isNavigationBarHidden = true
// 或 Objective-C
[navigationController setNavigationBarHidden:YES];
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "termsandConditionControl")
let nav = UINavigationController(rootViewController: vc)
nav.isNavigationBarHidden = true // 在這裡設定
window?.rootViewController = nav
window?.makeKeyAndVisible()
return true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: animated)
}
❌ 在 viewDidLoad 設定 isNavigationBarHidden 可能被後續 push/pop 動畫覆蓋。
✅ 使用 viewWillAppear/viewWillDisappear 配對管理,並開啟 animated: true 保證轉場平滑。
❌ 忘記在 Storyboard 勾選 Navigation Controller 的 "Shows Navigation Bar",雖代碼可覆蓋,但建議介面建構器與代碼一致。
iOS 13+ 可用 navigationBar.standardAppearance.configureWithTransparentBackground() 實現半透明/隱藏效果,同時保留狀態列內容。
核心記住:UINavigationController 實例方法 setNavigationBarHidden(_:animated:) 是官方標準 API,簡單可靠。