I have a tab bar which I only need on five screens. However, it goes to every screen when I push the view controller. How do I stop this from happening. I found a lot of solutions telling me to use hidesBottomBarWhenPushed but the problem I'm having with that is when I pop the view controller the tab bar is gone. How do I solve this problem? Also, please give me suggestions on my questions as I'm sort of new here! Thanks!
Edit: I've also seen this: self.tabBarController?.tabBar.hidden = false, but this kind of looks odd as the tab bar just disappears in the middle of the push animation.
Need to inherit UINavigationController custom navigation controller, override pushViewController & setViewControllers method to set hidesBottomBarWhenPushed, and then use custom navigation controller to jump
open class CustomNavigationController: UINavigationController {
...
open override func pushViewController(_ viewController: UIViewController, animated: Bool) {
if viewControllers.count > 0 {
viewController.hidesBottomBarWhenPushed = true
}
super.pushViewController(viewController, animated: animated)
}
open override func setViewControllers(_ viewControllers: [UIViewController], animated: Bool) {
if viewControllers.count > 1, let vc = viewControllers.last {
vc.hidesBottomBarWhenPushed = true
}
super.setViewControllers(viewControllers, animated: animated)
}
}
I actually figured it out. Basically in your viewdidAppear you add in self.hidesBottomBarWhenPushed = true and in your viewDidDisappear you add in
self.hidesBottomBarWhenPushed = false. Thanks for your guys's answers anyways.
Related
I have a standard UINavigationController for an iOS application in Swift 5 in which I'd like to hide the Navigation Bar on the first ViewController in the stack.
My storyboard is essentially:
UINavigationController -> ViewControllerA -> ViewControllerB
with simple Show segues.
In ViewControllerA I put the following:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.isHidden = true
}
In ViewControllerB I put the following:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.isHidden = false
}
But in the simulator the transition from A->B presents this weird, like, "scooping" animation and I cannot figure out how to get rid of it.
Thoughts? Thank you in advance!
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)
I have three viewControllers:
FeedController (UITabBar is visible)
PostController (UITabBar is hidden)
UserController (UITabBar is visible
I do this with the following code, from FeedController to PostController:
let postVC = PostController()
postVC.hidesBottomBarWhenPushed = true
pushViewController(postVC, animated: true)
postVC.hidesBottomBarWhenPushed = false
Then, from PostVC to UserVC:
let userVC = UserController()
userVC.hidesBottomBarWhenPushed = false
pushViewController(userVC, animated: true)
It works great. It shows the UITabBar everywhere except when navigating to a Post. However, the problem occurs when I go to a User Profile (UserController) from within a Post. It shows the UITabBar on the profile, as intended, but when I navigate back (using the back button in my UINavigationController) the UITabBar is still visible. I want it to be hidden again when I go back from the userVC to the postVC.
Is there any way I can accomplish this?
try in your post viewController:
override func viewWillDisappear(_ animated: Bool) {
postVC.hidesBottomBarWhenPushed = true
}
That will call it when the view is about to disappear but not when it appears so it should hide when you go back.
Now I have a viewController(A) which is root view controller for Navagation Controller followed by Tab Bar Controller, I want to perform segue from it to another viewController(B), totally replace the A.
Therefore I applied show detail(replace), it worked well as what I think in other cases. However in this case, when A segued to B, the navigation bar and Tab bar still existed. Why this happened and how to solve it?
In viewController(A) you have to write
override func viewWillAppear(_ animated: Bool) {
self.tabBarController?.tabBar.isHidden = false
self.navigationController?.navigationBar.isHidden = false
}
and in viewController(B) you have to write
override func viewWillAppear(_ animated: Bool) {
self.tabBarController?.tabBar.isHidden = true
self.navigationController?.navigationBar.isHidden = true
}
I wondering how Twiter is doing, in the ios app, to push a profile viewController with a new navbar or a new navigationController above the current viewController ?
In your stoyboard you have your UITabBarController connected to the UIUserController. Embed a UINavigationController between them and you get the desired result.
The UINavigationController is probably the same. What seems to be a custom navigation bar is actually a transparent navigation bar and a UIView coming from the top behind it.
You will need to hide the Navigation bar in viewWillAppear and show it for the pushed view controller
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
explanation can be found 👇
https://medium.com/#qbo/push-page-with-without-navigation-bar-eb3cea35178d
I am having multiple view controller in my application. I want to hide navigationbar in my first view controller. So I use the following code to hide the navigation bar
navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false, animated: true);
Now I want to add navigation bar in some other viewController but, my navigation bar not visible in that viewcontroller. Why it is happening?
My storyboard showing the navigation bar but once I try to run my application it is gone.
If I hide navigation bar from one view controller then we can't use navigation controller, Is it so? I hope I am wrong. Then what are the reasons for navigation bar not shown?
EDIT:
Also I want my view controller in portrait mode only. So I did the following Is that causing the issue?
extension UINavigationController{
public override func shouldAutorotate() -> Bool {
if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
return false
}
else {
return true
}
}
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.Portrait ,UIInterfaceOrientationMask.PortraitUpsideDown]
}
}
Edit 1:
I am using following code to move from one view controller not link from the storyboard. Is that causing issue now?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
presentViewController(secondViewController, animated: false, completion: nil)
Edit 2:
Please check my following screenshots. Which are my settings for secondview controller
Edit 3:
Here is my navigation controller attribute inspector
Navigation Controller is a controller, which has stack of view controllers. So if you have something like this:
NAV -> A -> (segue) B
Even if you'll hide navigation bar you still should be able to make segues. Also can't you just unhide navigation bar in second (B) view controller in viewWillAppear? And in first the same way hide it on viewWillAppear.
edit: Final solution to the problem:
Use:
let controller = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
self.navigationController!.pushViewController(controller)
instead of:
let secondViewController = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
presentViewController(secondViewController, animated: false, completion: nil)
Because pushViewController will add secondViewController to its stack. presentViewController was replacing your navigation controller that's why you couldn't see navigation bar.
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)
}
in viewDidLoad method of the view controller in which you don't want to show navigation bar add the line
navigationController.navigationBarHidden = true
you are presently hiding in all view controllers
Edit: You are presenting view controller instead it should be
self.navigationController!.pushViewController(controller)
do like in viewcontroller based hidden using Swift 4.0
To hide navigationController in viewWillAppear
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}
To unhide navigationController in viewWillDisappear
override func viewWillDisappear(animated: Bool)
{
super.viewWillDisappear(animated)
self.navigationController?.isNavigationBarHidden = false
}
I am having same requirement in my swift project.
this is how I have handled Navigation bar
Make sure your first screen is embedded into Navigation controller
example we have two screens A and B
In screen A you need to hide navigation bar in viewWillAppear
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
//hide navigation for screen A
self.navigationController?.navigationBarHidden = true
}
for enabling Navigation in screen B
you need to add below code in screen A
override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!)
{
if (segue.identifier == "screen B's segue identifier here")
{
//enable navigation for screen B
navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false, animated: true)
}
}
Using above style, I can enable or disable navigation bar for specific screen, whenever I want
If you need to have this navigation bar hidden only in this controller, the best way is to show it in viewWillDisappear() and hide in viewWillAppear().
It's too late to reply and there are other good answers but I would like to share what worked for me.
let controller = self.storyboard?.instantiateViewControllerWithIdentifier("HomeVC")
self.navigationController!.pushViewController(controller!, animated:true)