I am making a project where the first MainPage is TabBarController(MainTabController). But then I am passing to another Viewcontroller there is one more tabbetViewControllers(secondTabbedController). And now When I pass to secondTabbedController the tabs of MainTabController are not hiding. There Should be secondTabBarController Items but there tab items of first(MainTabBarController). I guess that it is because of the navigationController and If I delete it it is fixes. But I need this NavigationController. How to fix it ?
This is ArticlesViewController that You can find in first image:
Here is the solution:
write this code in first viewcontroller
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// this will show the tabbar when come back to first viewcontroller
self.tabBarController?.tabBar.isHidden = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// this will hide the tabbar when moved next viewcontroller
self.tabBarController?.tabBar.isHidden = true
}
Just true this "hidesBottomBarWhenPushed" when you are pushing view controller
let objViewController: ProductDetailsViewController? = UIStoryboard.mainStoryboard().instantiateViewController(withIdentifier: "ProductDetailsViewController") as? ProductDetailsViewController
objViewController?.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(objViewController ?? UIViewController(), animated: true)
And you can also set this in ViewController XIB "Attribute Inspector" by just clicking "Hide Bottom Bar on Push".
Related
I use Xcode 11.2 and the project minimum iOS deployment target is iOS 12.4.
I have a TabBarController on root page and on one of the tabs I have FirstViewController. When I push SecondViewController from FirstViewController, I want the tab bar to be hidden. I used hidesBottomBarWhenPushed property to hide the tab bar.
The tab bar is hidden when I push SecondViewController but when I pop the SecondViewController and move back to FirstViewController, the tab bar is still hidden.
I tried several ways to set hidesBottomBarWhenPushed to false when moving back to FirstViewController but none of the tries worked.
How can I re display tab bar when popped back to FirstViewController?
class FirstViewController: UIViewController {
#IBAction func buttonTap(_ sender: Any) {
let vc2 = SecondViewController()
// Set to Hide TabBar
hidesBottomBarWhenPushed = true
navigationController?.pushViewController(vc2, animated: true)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// This Does Not Work
hidesBottomBarWhenPushed = false
}
}
class SecondViewController: UIViewController {
/*
All The Followings Does Not Work
*/
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
hidesBottomBarWhenPushed = false
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
hidesBottomBarWhenPushed = false
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
hidesBottomBarWhenPushed = false
}
}
The key was to set hidesBottomBarWhenPushed to true from outside of the SecondViewController.
The code below was all I needed to write.
class FirstViewController {
func pushSecondViewController {
let vc = SecondViewController()
vc.hidesBottomBarWhenPushed = true // <- Here
navigationController?.push
navigationController?.pushViewController(vc, animated: true)
}
}
I attach the video of my issue. When i click on anywhere in viewcontroller navigation bar is appear
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.isNavigationBarHidden = true
self.navigationController?.hidesBarsOnTap = true
}
The above code added on viewwillappear its working on initially but when I click anywhere on screen navigationbar is appear.
Finally this solutions work for me
self.navigationController?.navigationBar.transform = CGAffineTransform(translationX: 0, y: -200)
Try Below code into ViewController you want to hide NavigationBar
DispatchQueue.main.async {
self.navigationController?.setNavigationBarHidden(true, animated: false)
self.view.isUserInteractionEnabled = true
//Below code conflicts with the hidden `NavigationBar` and make it visible on tap so set it false as below
self.navigationController?.hidesBarsOnTap = false
}
And ADD Below code in Other ViewController you want to show Navigationbar (Not in every other ViewController , just in ViewController you push or pop from thisViewController)
self.navigationController?.setNavigationBarHidden(false, animated: true)
Try with global queue
DispatchQueue.global().async {
navigationController?.setNavigationBarHidden(true, animated: animated)
}
or simple add this code in viewDidAppear
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: animated)
}
if this two won't work then check your view tap event may be there is some code added for navigation
Check out your main UINavigationController storyboard properties in storyboard and uncheck the "Hide bars when vertically compact", "Hide bars on tap". this causes the navigation bar to appear when click on view.
if you are creating UINavigationController programmatically then use following code.
UINavigationController().hidesBarsWhenVerticallyCompact = false
UINavigationController().hidesBarsOnTap = false
TRY TO BELOW
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.isNavigationBarHidden = true
}
override func viewWillDisappear(_: Bool) {
super.viewWillDisappear(true)
navigationItem.title = ""
}
I had a same problem too.Storyboard I deleted and recreated the viewController and it was fixed. For this reason I think the problem is related to the viewController not related to the NavigationController. I suggest you delete and recreate the viewController.
I have a UITabBarController. One of the tabs contains a UINavigationController.
I'd like to push a view controller onto the navigation stack and hide the tab bar on that view controller. I can do this easily with:
toVC.tabBarController?.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(toVC, animated: true)
or doing it in the storyboard:
The problem is, this hides the tab bar for any subsequent view controllers I push onto the stack. I'd like to simply hide the tab bar for this one view controller and show it for all other view controllers before and after it.
There is a workaround. It works the way it is presented on gif below.
For each UIViewController that is pushed into the UINavigationController stack I override the hidesBottomBarWhenPushed property this way:
override var hidesBottomBarWhenPushed: Bool {
get {
switch navigationController?.topViewController {
case .some(let controller):
switch controller == self {
case true:
return super.hidesBottomBarWhenPushed
case false:
return false
}
default:
return super.hidesBottomBarWhenPushed
}
}
set {
super.hidesBottomBarWhenPushed = newValue
}
}
The first switch checks whether this controller belongs to some UINavigationController stack. The second switch checks whether current top UIViewController of UINavigationController stack is self.
Hope it will work in your case. Happy coding (^
If you hide on the storyboard then by this property your tab bar will hide for all the view controllers. So you can manage this by code.
You can do this programmatically by just writing one line of code in ViewDidLoad() or ViewWillAppear() method
For Swift 3:-
self.tabBarController?.tabBar.isHidden = true
And where you want to unhide the tab bar just write the following code in ViewDidLoad () or ViewWillAppear() method
self.tabBarController?.tabBar.isHidden = false
Try this in the view controller you want to hide the tab bar in:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = true
}
And this in the view controllers before and after the one you want to hide the tab bar in:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = false
}
EDIT:
Fully implemented example:
class ViewController1: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = false
}
}
class ViewController2: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = true
}
}
class ViewController3: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = false
}
}
I'm modally presenting a viewController with a semi-transparent view. It's a custom activity indicator. I would like it to cover the view, but leave the navigation bar and tab bar visible and accessible.
The docs, and several SO answers (e.g. Presenting a Modal View Controller hides the Navigation Bar) seem to suggest that presenting the modal onto the navigation controller should achieve this. But when I do it, it shows the tab bar correctly, but covers the navigation bar.
Any ideas? Here is the relevant code:
let spinnerVC = SpinnerViewController()
spinnerVC.modalPresentationStyle = .overCurrentContext
spinnerVC.modalTransitionStyle = .crossDissolve
self.navigationController?.present(spinnerVC, animated: true, completion: nil)
//self.navigationController is definitely not nil
You can present a your viewcontroller by adding as rootViewController of a navigationController and then present it over the current viewController like this:
let spinnerVC = SpinnerViewController()
let navVC = UINavigationController(rootViewController:spinnerVC)
navVC.modalPresentationStyle = .overCurrentContext
navVC.modalTransitionStyle = .crossDissolve
self.present(navVC, animated: true, completion: nil)
you could do it two ways:
first one:
Put this code in your parent view
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)
}
second one:
Add a reference to your invoker in your alert controller in order to hide the bar like this:
weak var invokerView : UIViewController?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.invokerView?.navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.invokerView?.navigationController?.setNavigationBarHidden(false, animated: animated)
}
Don't present it. Add it as a child view controller to the top view controller of your navigation controller and add its view as a subView to the view of the same adjusting the frame.
let spinnerVC = SpinnerViewController()
spinnerVC.view.frame = self.navigationController?.topViewController?.view.bounds
self.navigationController?.topViewController?.addChildViewController(spinnerVC)
self.navigationController?.topViewController?.view.addSubview(spinnerVC.view)
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