How to hide a navigation bar from first ViewController in Swift?
If you know that all other views should have the bar visible, you could use viewWillDisappear
to set it to visible again.
In Swift:
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)
}
iOS: Hide the navigation bar for only one viewcontroller which is root of the UINavigationController?
I got the another way to hide/show navigationbar from one of my friend.
- Set a delegate for the NavigationController:
navigationController.delegate = self
- Hide/Show navigationbar for each ViewController all in one place
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
let hide = (viewController is YourVC)
navigationController.setNavigationBarHidden(hide, animated: animated)
}
hide Navigation Bar on root view controller and show it otherwise - Swift
I solved it with this code:
override func viewWillDisappear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(false, animated: animated);
super.viewWillDisappear(animated)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}
How to hide the navigation bar shadow only in detail view?
For Generic flow
You could use this setup. Say ViewController
is the all other view controller class (where you want the shadow), and DetailViewController
is the detail view controller class.
The thing i'm doing preserving the shadow image.
ViewController.swift
class ViewController: UIViewController {
var shadowImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
shadowImage = self.navigationController?.navigationBar.shadowImage
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.shadowImage = shadowImage
}
}
And DetailViewController.swift
class DetailViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.navigationController?.navigationBar.shadowImage = UIImage()
}
}
Storyboard setup
Output
Note: Another neat approach would be storing the shadow within the DetailsViewController
and setting it while the view is about to disappear
class DetailsViewController: UIViewController {
var shadowImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
shadowImage = self.navigationController?.navigationBar.shadowImage
self.navigationController?.navigationBar.shadowImage = UIImage()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.shadowImage = shadowImage
}
}
This solution is more elegant and resulting in a clean management.
For MasterDetailFlow, Using SplitViewController
In your MasterViewControlelr.swift
override func viewWillAppear(_ animated: Bool) {
clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed // placeholder code when you created the project
super.viewWillAppear(animated)
self.navigationController?.navigationBar.shadowImage = nil
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.shadowImage = UIImage()
}
In your DetailViewController.swift
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.shadowImage = UIImage()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.shadowImage = nil
}
Output(Master/Detail flow)
Hide Navigation Bar for a View Controller
Use code:- Swift 5
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
// Hide the Navigation Bar
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
// Show the Navigation Bar
self.navigationController?.setNavigationBarHidden(false, animated: false)
}
I think you have done mistake in animated: true
Hide iOS NavigationBar on one ViewController - weird animation happening
There's another method for hiding the navigation bar where you can set 'animated' to false which might help you.
self.navigationController?.setNavigationBarHidden(true, animated: false)
How can I hide navigation bar of a specific View Controller?
You are making mistake, in question you have set false
in viewDidLoad
to hide navigationBar
, you need to set true
instead of false
, also try on viewDidAppear
.
self.navigationController?.setNavigationBarHidden(true, animated: true)
Related Topics
Unbalanced Calls to Begin/End Appearance Transitions for <Uitabbarcontroller: 0X197870>
Uitableviewcell with Autolayout Left Margin Different on iPhone and iPad
iOS 11 Customise Search Bar in Navigation Bar
How to Use Uiscrollview in Interface Builder
Auto Layout in Uicollectionviewcell Not Working
Perform Segue Programmatically and Pass Parameters to the Destination View
How to Add "Done" Button to Numpad in iOS Using Swift
Secure Keys in iOS App Scenario, Is It Safe
How to Check If a File Exists in the Documents Directory in Swift
How Big Should a Uibarbuttonitem Image Be
iOS 10 Error [Access] <Private> When Using Uiimagepickercontroller
How to Read Data Structure from .Plist File into Nsarray
Implement Document Picker in Swift (Ios)
How to Differentiate Between Iphone4 and iPhone 3
What's the Difference Between "Architectures" and "Valid Architectures" in Xcode Build Settings
Updating iOS Badge Without Push Notifications