So I'm following this tutorial on UISearchController, and modified it slightly. I created a RedViewController before the SearchViewController, in which the navigation bar is hidden. As you can see in the animation, unwinding from the SearchViewController to the RedViewController isn't kind to the search bar. Additionally, if you don't complete the unwind segue (by swiping back, then forward), the entire search bar disappears. I tried dismissing the search controller on viewWillDisappear, but it didn't work.
Animation
SearchViewController:
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Candies"
navigationItem.searchController = searchController
definesPresentationContext = true
}
override func viewWillDisappear(_ animated: Bool) {
navigationItem.searchController?.dismiss(animated: false)
}
RedViewController:
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)
}
Related
I want to hide the navigationbar for only one viewcontroller which is the root viewcontroller of the UINavigationController.
Currently I am using below code to hide the navigation bar for a particular viewcontroller.
To hide the navigationbar,
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.isNavigationBarHidden = true
super.viewWillAppear(animated)
}
To show the navigationbar for other viewcontrollers,
override func viewWillDisappear(_ animated: Bool) {
self.navigationController?.isNavigationBarHidden = false
super.viewWillDisappear(animated)
}
When I am trying to use this code, the app is being crashed in iOS 13 devices because of threading violation: expected the main thread.
Please checkout the issue which I am getting when I use the above code to hide the navigationbar,
iOS 13: threading violation: expected the main thread
Please let me know if there is any other way to hide the navigationbar for only one viewcontroller.
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)
}
import UIKit
class ViewController: UIViewController {
override func viewWillAppear(_ animated: Bool){
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}
override func viewWillDisappear(_ animated: Bool){
super.viewWillDisappear(animated)
self.navigationController?.isNavigationBarHidden = false
}
}
You can make it transparent (Completely invisible) when viewWillApper get called and back to normal when view willDisappear get called. Here are helper functions.
func makeNaBarTransparent() {
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
}
func restoreNavigationBarToDefault() {
navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
navigationController?.navigationBar.shadowImage = nil
}
USAGE
import UIKit
class ViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
makeNaBarTransparent()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
restoreNavigationBarToDefault()
}
}
I have a UITabBar. In one tab is a UINavigationController. Let's say the 2nd or 3rd UIViewController in the stack has this:
class ChildVC: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: false)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: false)
}
}
If you click the current tab it will popToRootViewController() on the navigation controller. The problem is, in viewWillDisappear(:) of my current tab the navigationController is nil. So the navigationBar remains hidden.
What's the proper way to handle this? Should I just set the navigation bar to visible in the root view controller's viewDidAppear? That seems hacky.
If anybody else sees this, I don't know why the reference to self.navigationController gets set to nil before viewWillDisappear when you popToRootViewController() but a workaround I found was just to store your own reference to it.
class ChildVC: UIViewController {
private weak var navCtrl: UINavigationController?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navCtrl = navigationController
navCtrl?.setNavigationBarHidden(true, animated: false)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navCtrl?.setNavigationBarHidden(false, animated: false)
}
}
You should override the viewWillAppear in the rootViewController and setNavigationBarHidden from there. navigationController is nil at viewDidDisappear because it has already been popped off the navigation stack.
What I am trying is to set a ViewController(root) with a NavigationController that will connect with three ViewController.
Two of the linked ViewController have to have a NavigationBar on the top of each screen. The other one do not have to have the Navigation bar. Further, the root View Controller do not have to have a Navigation bar.
I hide the NavigationBar on the root View Controller as follows:
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(true, animated: animated)
super.viewWillAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(false, animated: animated)
super.viewWillDisappear(animated)
}
but I am not able to hide the Navigation bar on the linked View Controller that does not have to have the Navigation bar.
I have also tried on the viewDidLoad function of the View Controller in which I want to hide the Navigation bar using:
self.navigationController?.setNavigationBarHidden(false, animated: true)
but the Navigation bar is still being shown.
How can I hide the Navigation bar on a specific View Controller?
Thanks in advance!
You can Try like this:-
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController!.navigationBarHidden = true
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController!.navigationBarHidden = false
}
You are making mistake, in question you have set falsein viewDidLoad to hide navigationBar, you need to set true instead of false, also try on viewDidAppear.
self.navigationController?.setNavigationBarHidden(true, animated: true)
Use below code in viewDidAppear method
self.navigationController?.setNavigationBarHidden(true, animated: true)
Try this code in viewDidAppear :-
self.navigationController?.navigationBarHidden = true
i have 2 ViewController
VC A and VC B
VC A => NavigationBar Hidden = true
VC B => NavigationBar Hidden = false
I make a segue from A => B, but the navgiationbar in VC B is not visible.
i have include the following swift code in vc b:
override func viewWillAppear(animated: Bool) {
self.navigationController?.navigationBarHidden = false
}
Any ideas?
if you are using
self.navigationController?.navigationBar.hidden = true;
use this to show Bar
self.navigationController?.navigationBar.hidden = false;
not to use
self.navigationController?.navigationBarHidden = false;
please check that
The navigation bar and the tool bar should disappear in the storyboard when you change the segue -- that's normal.
Try checking
Following should work with iOS 8 for a particular view
override func viewWillAppear(animated: Bool)
{
self.navigationController?.navigationBarHidden = false
}
To show on all viewControllers place it in viewDidLoad
self.navigationController?.navigationBarHidden = false
You can do following work around
in your VC A viewWillDisappear
override func viewWillDisappear(animated: Bool) {
self.navigationController?.navigationBarHidden = false
}
Do it this way:
In your VC A use this code:
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setNavigationBarHidden(true, animated: true)
}
And in VC B use this code:
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setNavigationBarHidden(false, animated: true)
}
VC A and VC B embedded in Navigation controller here and I have this working.
In VC A i have the following code:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
navigationController?.setNavigationBarHidden(false, animated: true)
}
and in B I have
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
navigationController?.setNavigationBarHidden(true, animated: true)
}
works perfectly
How can I hide a navigation bar from first ViewController or a particular ViewController in swift?
I used the following code in viewDidLoad():
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.isNavigationBarHidden = true
}
and also on viewWillAppear:
override func viewWillAppear(animated: Bool) {
self.navigationController?.isNavigationBarHidden = true
}
Both methods hide the navigation controller from all ViewControllers.
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)
}
Swift 3
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Hide the navigation bar on the this view controller
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Show the navigation bar on other view controllers
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
You can unhide navigationController in viewWillDisappear
override func viewWillDisappear(animated: Bool)
{
super.viewWillDisappear(animated)
self.navigationController?.isNavigationBarHidden = false
}
Swift 3
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
You could also create an extension for this so you will be able to reuse the extension without implementing this again and again in every view controller.
import UIKit
extension UIViewController {
func hideNavigationBar(animated: Bool){
// Hide the navigation bar on the this view controller
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}
func showNavigationBar(animated: Bool) {
// Show the navigation bar on other view controllers
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
}
So you can use the extension methods as below
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
hideNavigationBar(animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
showNavigationBar(animated: animated)
}
In Swift 3, you can use isNavigationBarHidden Property also to show or hide navigation bar
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Hide the navigation bar for current view controller
self.navigationController?.isNavigationBarHidden = true;
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Show the navigation bar on other view controllers
self.navigationController?.isNavigationBarHidden = false;
}
Ways to hide Navigation Bar in Swift:
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
self.navigationController?.isNavigationBarHidden = true
Ways to show Navigation Bar in Swift:
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController?.navigationBar.isHidden = false
self.navigationController?.isNavigationBarHidden = false
private func setupView() {
view.backgroundColor = .white
navigationController?.setNavigationBarHidden(true, animated: false)
}
Alternative
in viewDidLoad use this settings
title = "Madman"
navigationController?.isNavigationBarHidden = false
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always
Check the constraints of Collectionview, scrollview or tableView
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
Want to Hide the NavigationBar on the First ViewController
override func viewWillAppear(animated: Bool) {
self.navigationController?.isNavigationBarHidden = true
}
Want to Show the NavigationBar on the Second ViewController
override func viewWillAppear(animated: Bool) {
self.navigationController?.isNavigationBarHidden = false
}
/*. Swift 5 */
let controller = self.storyboard?.instantiateViewController(withIdentifier: "sc_userNavigation") as! UserNavigationViewController
let navigationController = UINavigationController(rootViewController: controller)
navigationController.setNavigationBarHidden(true, animated: false)
navigationController.modalPresentationStyle = .fullScreen
self.present(navigationController, animated: false, completion: nil)
In IOS 8 do it like
navigationController?.hidesBarsOnTap = true
but only when it's part of a UINavigationController
make it false when you want it back
I use a variant of the above, and isolate sections of my app to be embedded in differing NavControllers. This way, i don't have to reset visibility. Very useful in startup sequences, for example.
Call the set hide method in view Will appear and Disappear. if you will not call the method in view will disappear with status false.It will hide the navigation bar in complete navigation hierarchy
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated:true)
}
You can do it from the window controller (Swift3)
class WindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
window?.titleVisibility = .hidden
}
}