Appearing/disappearing UITabbar with navigation - ios

I have a UITabBar with four tabs. My second tab has two child navigation controller like 2nd tab -> child VC1 -> child VC2.
Now when I go to child VC1, i want to disappear/hide the bottom tabbar & if I go to child VC2 from VC1, the bottom tabbar should be shown again.
If I back from VC2 to VC1, the tabbar will be disappear and then again back from VC1 to 2nd tab controller, the tabbar should be appear again.
How can I achieve this thing ?
In the storyboard, I selected "Hide Bottom Bar on Push" for the child VC1, the tabbar is disappear on this controller but the problem is when I go to child VC2 from VC1, the tabbar still disappear.
How can solve this issue ? Thanks.

In VC1
-(void)viewWillAppear:(BOOL)animated {
self.tabBarController.tabBar.hidden = YES;
}
In VC2
-(void)viewWillAppear:(BOOL)animated {
self.tabBarController.tabBar.hidden = NO;
}

Related

TabBar is hidden after going back to the initial ViewController of a TabBarController Using Segue

I have a tab bar controller with three table view controllers and the second VC is embedded in a navigation Controller. in the second VC, I made the tabBar hidden using this line self.tabBarController?.tabBar.isHidden = true and I created a bar button to go back to the first view controller which is the "home" VC using segue with modal presentation.
Screenshot of my StoryBoard
My problem is after hitting the back button and going back to home VC from the second VC, the tabBar is still hidden even though I put self.tabBarController?.tabBar.isHidden = false in the home VC's viewWillAppear method and the second VC's viewWillDisappear method.
Here is the result that I expected vs what I got
expected home VC
result home VC
How can I make the Tab Bar show?
When you are using the modal presentation segue, you are creating a completely new instance of HomeViewController. The new HomeViewController is not linked to the TabBarController in your hierarchy.
Here's you initial view hierarchy:
TabBarController
-> HomeVC
-> CreateVC (Navigation Controller)
-> CreateQuizVC
-> SavedVC
Now after tapping the back button you'll get the following:
TabBarController
-> HomeVC
-> CreateVC (Navigation Controller)
-> CreateQuizVC
-> HomeVC(2)
-> SavedVC
What you could do is, instead of using the segue to go back, add an IBAction in your code to set the selectedIndex of the TabBar programatically, and link the Back UIBarButtonItem to this IBAction.
#IBAction func backButtonAction(_ backButton: UIBarButtonItem) {
// Keep in mind that the CreateQuizVC is embeded in a NavigationController.
// The NavigationController is the child of the TabBarController
navigationController?.tabBarController?.selectedIndex = 0
navigationController?.tabBarController?.tabBar.isHidden = false
}
However, my suggestion is you use the TabBar as it's intended by Apple. Don't hide it while you're presenting your CreateQuizVC, and use the TabBar to navigate between the tabs. This will help with user experience, since everybody on iOS is expecting this behaviour from a TabBar.

Navigation bar disappear after create viewcontroller dynamically

I'm new to iOS and trying to build one AR app with navigation bar, I defined 3 viewcontroller in the app, and using storyboard and navigation bar to switch the viewcontrollers:
VC1 - Home view, there is one button navigated to VC2;
VC2 - this view controller will call camera to scan image marker; when the image was identified, I just instantiate VC3 programmatically.
VC3 - just showing some information for the image, I added 2 buttons here which will navigated to VC1 and VC2 seperately.
So VC1->VC2, VC2->VC1 are OK as the navigation bar configuration, the problem is when I click button in VC3 to VC1 or VC2, both navigation bar in VC1 and VC2 disappear. It seems I missed some configuration here, can anyone tell me how to make the navitation bar always there?
You can check navigationBar's visibility in method viewDidAppear of VC1 and VC2 like this:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(#"navigationBar is hidden:%#\n",
self.navigationController.navigationBar.hidden ? #"YES": #"NO");
}
It should be YES in VC1 and VC2, you probably change navigationBar's visibility in somewhere, find it then fix it.
I have solved my problem from below link, it is working perfectly
Change destination of Navigation Back button

Tab bar missing when a segue is added

So I have 3 VCs embedded in a navigation controller and a tab bar controller. However, whenever I add a segue from the 3rd VC to the 1st VC, the navigation bar and tab bar disappear from the storyboard. So I tried adding a segue programmatically on the 3rd VC swift file as such:
let collectionVC = self.storyboard?.instantiateViewController(withIdentifier: "collectionVC") as! CollectionViewController
let navigationVC = UINavigationController(rootViewController: collectionVC)
self.present(navigationVC, animated: true, completion: nil)
Unfortunately, with the added code above, the tab bar is still missing but the navigation bar is there. I hope someone could help me.
If I understood correctly your problem, you are using a UINavigationController and you are trying to go from VC3 to VC1, which means that you have a navigation stack like this:
VC1 -> VC2 -> VC3
If you want to go back to VC1, you can use:
navigationController?.popToRootViewControllerAnimated(true)‌​
You will still have your tab bar, your navigation bar and - most important - you won't create another instance of VC1. This is the correct - and easiest - way of dealing with a UINavigationController.

Different UIBarButtonItems if Pushed or Modal

I am trying to use different Navigation Bar Button Items for a single View Controller.
The View Controller can be :
pushed inside a Navigation Controller
presented modally inside a navigation controller
If it's presented modally, I need a close left bar button to dismiss the modal.
Is there a way to know if the VC is presented modally in order to set a dismiss left bar button accordingly?
I solved this problem by implementing such a method on UIViewController category
and then using this method in viewDidLoad to determine, whether the current controller presented modally or by pushing into navigation controller:
- (BOOL)isModal {
if (self.viewController.navigationController && self.viewController.navigationController.viewControllers.firstObject == self.viewController) {
return YES;
}
return NO;
}

Is there a way to pop back to previous navigation controller?

I have a base nav controller which has a tab bar controller nested inside. inside that tab bar controller is another nav controller nested in it. If I'm in the child nav controller I can pop to root view in one of the views but it only takes me to the root view of the CHILD nav controller. Is there a way to pop back to the first nav controller?
If you are using storyboard then you can use Unwind segue approach which always works for me
Just Control + drag from "Pop to Main Screen" UIButton to Exit (green button at bottom of scene) select unwind method declared in first screen with title "Main" e.g.
-(IBAction)customUnwind:(UIStoryboardSegue *)sender
{
NSLog(#"Unwind successful");
}
Try this,
You can pop to root view from the child nav controller by
UITabBarController *tc = (UITabBarController *)self.navigationController.parentViewController;
[tc.navigationController popToRootViewControllerAnimated:YES];

Resources