I want to hide the navigationBar
for the first ViewController
which is my first screen. It's all embedded within a navigation controller. I tried the below code but not working.
override func viewWillAppear(_ animated: Bool) {
let navigationBar = navigationController?.navigationBar
if #available(iOS 13.0, *) {
let navigationBarAppearence = UINavigationBarAppearance()
navigationBarAppearence.shadowColor = .clear
navigationBar?.scrollEdgeAppearance = navigationBarAppearence
navigationBar?.standardAppearance = navigationBarAppearence
navigationBar?.compactAppearance = navigationBarAppearence
navigationBar?.backgroundColor = .clear
navigationBar?.isHidden = true
} else {
navigationBar?.isHidden = true
}
}
I'm using xcode11 How can I hide the navigation bar? Suggestions are always appreciated.
试试这个代码
self.navigationController?.isNavigationBarHidden = true
Try this
navigationController?.isNavigationBarHidden = true
instead of
let navigationBar = navigationController?.navigationBar
navigationBar?.isHidden = true
I know you tried with coding but try something different. Without code, you can use Main.storyboard.
1.Press on your "Navigation Controller Scene" 2.Press "Navigation Controller" 3.Your side bar to your righthand side (forgot what its called) 4.Hover over the top icons to find "show the Attributes inspector" 5.After find under the "Simulated Metric" is the "Navigation Controller" 6.Next to "Bar Visbilty" unclick "Shows Navigation Bar" 7.All done
I know it is not code but hope this works for you.
hide navigation bar for in iOS15 + xcode 13
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 15, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.backgroundColor = UIColor(red: 0.0/255.0, green: 125/255.0, blue: 0.0/255.0, alpha: 1.0)
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
}
You need to hide in each view controller. This code can help you.
// Hide navBar when view controller appear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: animated)
}
// Show navBar again when view controller disappear
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: animated)
}
Note: You need to display the navBar again when disappear, because if you set this setting in one view controller, all other view controllers will be affected.
Tested with XCode Version 13.3.1 (13E500a) and iOS 15.1 (19B74) on iPhone 6s.
Use ViewController's viewDidLayoutSubviews method to hide/show the navigationbar.
FirstViewController
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
SocondViewController
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.