I am presenting a loading screen where I do not want the navigation bar to be shown. So I use
self.navigationController.navigationBar.hidden = true
Which does the trick and hides the navigation bar. But when I want to show the navigation bar, I want to animate it in.
I have tried using this code but the bar does not appear.
self.navigationController.setNavigationBarHidden(false, animated: true)
After running the above code the bar is still hidden, how can I show/animate the bar?
I think your method is correct already. You just need to know where to put the code. Try the following code.
Code for ViewController 1
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBarHidden = true;
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.navigationBarHidden = true;
}
#IBAction func buttonTapped(sender: UIButton) {
self.performSegueWithIdentifier("goToScreen2", sender: self)
}
}
Code for ViewController 2
import UIKit
class ViewController2: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
}
Update Answer:
I am able to unhide the navigation bar using the following code.
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBarHidden = true;
}
#IBAction func buttonTapped(sender: UIButton) {
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
Screen shot of the implementation:-
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 several views in my app, and I only want a navigationbar on one of them.... I used a navigationcontroller and at first I was using this code (while my app was in its infancy and only had 2 views)
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)
}
It worked fine - however, the app has become more complex - I have these views
lazy var orderedViewControllers: [UIViewController] = {
return [self.newVc(viewController: "pageOne"),
self.newVc(viewController: "pageTwo"),
self.newVc(viewController: "pageThree"),
self.newVc(viewController: "pageFour"),
self.newVc(viewController: "activate")
]
}()
Where this code isn't applied to, even if I create a custom view controller for each view.
I thought the way to do this would be to put the top chunk of code in every view, but it's not working for the bottom chunk. In essence my question is how do I use NavigationController to create a bar ONLY on one view.
One option: use a "base view controller" class which handles hiding / showing the Navigation Bar, and make your "pages" sub-classes of the "base" class.
import UIKit
class BaseViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
}
class ViewController: UIViewController {
// has buttons with
// Show (e.g. push)
// segues to Settings, First, Second, Third view controllers
}
class SettingsViewController: UIViewController {
// Settings VC is a normal UIViewController, because
// we *want* the NavBar to remain visible
}
class FirstViewController: BaseViewController {
#IBAction func backTapped(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
}
class SecondViewController: BaseViewController {
#IBAction func backTapped(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
}
class ThirdViewController: BaseViewController {
#IBAction func backTapped(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
}
You can use this method of UINavigationControllerDelegate
optional func navigationController(_ navigationController: UINavigationController,
willShow viewController: UIViewController,
animated: Bool){
if viewController == self."desired view controller" {
self.isNavigationBarHidden = true
}else{
self.isNavigationBarHidden = false
}
}
Thank you all for the support. I have resolved my issue by doing the following:
I put the only view controller I wanted to have a navigation bar in a navigation controller view the Embed menu.
I added a custom back button.
I have one View Controller and this View Controller contains two views/scenes in the main.storybard.
I am trying to hide the top navigation bar at first view/scene, but unhide it again on the second view/scene.
I tried with
self.navigationController?.isNavigationBarHidden = true
But this will only work with two View Controller classes.
Does anyone have a idea to manage it?
Hide navigationbar in viewWillAppear & unhide in viewWillDisappear
var shouldHideNavBar = false
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(shouldHideNavBar, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if shouldHideNavBar == true {
navigationController?.setNavigationBarHidden(false, animated: animated)
}
}
And when you perform segue set shouldHideNavBar as true
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "show") {
let viewController = segue!.destinationViewController as! ViewController
viewController.shouldHideNavBar = true
}
}
You should use extra control. Or you can create an IBInspactable variable and assing it's value on Interface Builder. Like this:
#IBDesignable class myViewController: UIViewController{
#IBInspectable var isNavbarHidden: Bool = true{
didSet{
self.navigationController?.isNavigationBarHidden = isNavBarHidden
}
}
override func viewDidLoad(){
super.viewDidLoad()
//I am not sure if this line is necessary
self.navigationController?.isNavigationBarHidden = isNavBarHidden
}
}
After then go to InterfaceBuilder(your storyboard file) and set its value for your Scenes on your viewControllers properties.
I want to hide the navigation bar for the first view controller and show for the rest. In order to achieve this I wrote the following code :
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBarHidden = true
}
override func viewWillDisappear(animated: Bool)
{
super.viewWillDisappear(animated)
self.navigationController?.navigationBarHidden = false
}
After writing this code it works fine, i.e this view controller does not show the navigation bar and the rest show as desired. But after writing this code another problem arise which is as follows:
problem link.
According to the solution given on the above link I need to remove the code:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBarHidden = true
}
Which brings me back to problem 1
Can anybody help to get rid of both issues?
Use this instead of navigationBarHidden:
self.navigationController?.setNavigationBarHidden(true, animated: animated)
In your SecondViewController add this code:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBarHidden = false
}
On the View you want to hide navigation bar. put this code.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBarHidden = true;
print("Navgition bar hidden")
}
On the next View, from where you want to show the navigation bar. put below code.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBarHidden = true;
print("Navgition bar show")
}
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