Tab bar is hidden in iOS - ios

I have a tab bar controller with four tabs. For one tab, i have created a new storyboard with navigation view controller and simple UIViewController. For other tabs, connected view controllers with navigation controller within the same storyboard.
When i try to launch first view controller from new storyboard from first tab, it is not showing tab bar. for others it is showing tab bar properly.
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSInteger index = [tabBar.items indexOfObject:item];
if(index == 0){
UIViewController *suggestionsViewCintroller = (UIViewController *)[[UIStoryboard storyboardWithName:#"suggestions_view" bundle:nil] instantiateViewControllerWithIdentifier:#"suggestions_view_controller"];
[self addChildViewController:suggestionsViewCintroller];
[self.view addSubview:suggestionsViewCintroller.view];
suggestionsViewCintroller.hidesBottomBarWhenPushed = NO;
[suggestionsViewCintroller didMoveToParentViewController:self];
}
}
Navigation controller configuration in storyboard :
View controller and tab bar controller are in different storyboards.
Why is it not showing tab bar in view?

I don't think that is the proper way to add a view controller to the tab bar. I think you want to sub class your UITabBarController and add the new view controller to the UITabBarController sub class in the viewDidLoad method of the sub class. You can see an example of this here. The main code is added in TabBarController (a sub class of UITabBarController)
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationController* naviController = [[UIStoryboard storyboardWithName:#"Other" bundle:nil]
instantiateViewControllerWithIdentifier:#"NavigationController"];
naviController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Suggestions"
image:[UIImage imageNamed:#"suggestions"] tag:2];
self.viewControllers = [self.viewControllers arrayByAddingObject:naviController];
}
Edited the example to use 2 different storyboards.
Main storyboard:
Other storyboard:
Video here.

Calling 'addChildViewController' you do not add tab in UITabBarController.
This call just add child view controller over whole view of UITabBarController. So it is quit reasonable, that you can not see tabbar.
If you wish to add view controller as TAB of UITabBarController, you should use 'viewControllers' property as shown here:
https://stackoverflow.com/a/11399634/4322841
And should maybe use 'selectedIndex' property.

Related

ios: TableViewControl in TabViewControl the navigation bar overlaps table view

The root View View controller is Navigation controller and its first level is a TabViewController.One of the tab item is a TableViewController.
Here is the relationship:
However the navigation bar overlap the table view:
I have also set simulated metrics,So what can be the problem??
Thanks for any help.
Simulated metrics are just that, simulated. They do not actually apply to the compiled product.
To fix this, I find it easiest to set the edgesforextendedlayout with the various values of edge values. Usually all but the top.
The rootViewController should be the UITabBarController. Follow this code:
1.Make the UITabBarController the rootViewController in the application delegate or in your main.storyboard set it as the initial View Controller.
2.In the UITabBarController.m place this code there to create the UINavigationController with a UIViewController embeded inside of it.
//Inside UITabBarController.m
- (void)viewDidLoad {
[super viewDidLoad];
UIViewController *vc = [[UIViewController alloc]init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:vc];
self.viewControllers = #[navCon];
}
Get rid of the navigation controller in the beginning and, instead, embed the tableviewcontroller inside a navigation controller.
(Select the view controller -- Click "editor" > "embed in" > "navigation controller").
Make sure the tab bar controller is the root view controller as well
This will also fix the overlapping issue

Pushing a view controller onto a navigation controller from a tab bar button

I have a tab bar controller with a button like so:
- (void) addButtonPressed:(UIButton *) sender
{
[sender setBackgroundColor:[UIColor regularColor]];
PostViewController *post = [[PostViewController alloc] init];
[self.navigationController pushViewController:post animated:YES];
}
This code runs but the PostViewController is never shown and the tab bar controller remains.
How do I get to push to a new controller?
The NavigationController was created and StartViewController was add as rootController.
Then in StartViewController I have:
TabBarController *tab = [[TabBarController alloc] init];
// Presentation
[self presentViewController:tab animated:NO completion:nil];
in tab bar you need to create separate Navigation Controllers.
Suppose there are 3 tabs A, B and C. All the three tabs have functionality of navigation from one view to another. than you need to create three separate Navigation controllers eact pointing to Tab A, B and C. In this way you can navigate to any class within the specific Tab.
Check out this link for more details.
Hope this will help you. Happy coding :)
You may need to embed your tab bar controller within a navigation controller, In your storyboard click on your tabbarController so that it is highlighted with all the blue lines and then go to Editor in Xcode choose embed IN> UINavigationController ..If you want to do it programatically then in your AppDelegate where you setup your Window just Use This::
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:tabBarController];
navigationController.toolbarHidden=YES;
navigationController.navigationBarHidden=YES;
self.window.rootViewController =navigationController;
if you are using .xib then you have to use NSBundle to load nib
if you are using storyboard then you need to use prepareForSegue to pass on data or just display it out

How to move viewcontroller to rootViewController on tabbar item click

I am working with UITabbarController and UINavigationController for my application. And i have created application with UITabbar base application it that i am giving different type of navigation controller to navigate view. As given below image.
But i have 1 problem which i describe as follows:
Suppose i have 5 ViewController and 3 NavigationViewController for that all five view controllers like View1 as rootViewController and view2 as subview. view 3 as RootViewController for 2nd NavigationViewController and view 4 as subview of 3rd RootViewController and others.
When i run application at that time view1 loaded as RootViewController now i am navigation to view 2 as it's subView of view 1 at that time my tab bar selected to first tab. When i click on 2nd tab bar button it shows me view 3 as RootViewController for 2nd NavigationController.
And now i am clicking back to 1st Tabbar button to view view 1 but it shows me subview view 2. If i need to move to RootViewController i need to press back button to View my RootView.
So according to my sinario is it possible to set RootView for that particular tab bar so user can easily go to RootView without viewing subviews for all tab bar click items.
Please help me.
Create the three view controllers(You five as your requirement), and then give each view control to the each separate navigationcontrollers. and then assign three navigationcontrollers to the tabbar as follows-
RideViewController* rideObj = [[RideViewController alloc]initWithNibName:#"RideViewController" bundle:nil];
RequestARideViewController* requestARideObj = [[RequestARideViewController alloc]initWithNibName:#"RequestARideViewController" bundle:nil];
MyAccountViewController* myAccntObj = [[MyAccountViewController alloc]initWithNibName:#"MyAccountViewController" bundle:nil];
navCtrlObj1 = [[UINavigationController alloc]initWithRootViewController:rideObj];
navCtrlObj2 = [[UINavigationController alloc]initWithRootViewController:requestARideObj];
navCtrlObj3 = [[UINavigationController alloc]initWithRootViewController:myAccntObj]
self._tabBarController = [[UITabBarController alloc]init];
self._tabBarController.delegate=self;
self._tabBarController.viewControllers = [NSArray arrayWithObjects:navCtrlObj1,navCtrlObj2,navCtrlObj3,nil];
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
write your code here to move the ViewController as written below.(as your requirement)
[navCtrlObj1 popToRootViewControllerAnimated:YES];
[navCtrlObj2 popToRootViewControllerAnimated:YES];
[navCtrlObj3 popToRootViewControllerAnimated:YES];
}
use this code. When ever you are clicking the tab you be shown the root view of the navigationcontroller of the tab
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
write your code here to move the ViewController as written below.(as your requirement)
[navcontrol1 popToRootViewControllerAnimated:YES];
[navcontrol2 popToRootViewControllerAnimated:YES];
[navcontrol3 popToRootViewControllerAnimated:YES];
}
Create the three view controllers, and then give each view control to the each separate navigationcontrollers. and then assign three navigationcontrollers to the tabbar.
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController setDelegate:self];
self.tabBarController.viewControllers = #[navigationController1, navigationController2,navigationController3];
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
write your code here to move the view
}

Add TabBArController to the UIView

Adding tab bar controller to the UIView by [self.view addSubview:tabBarController.view];
is not loaded the content view controllers view, while selecting the tab bar items it crashed. When set the tab bar controller to the root view controller its working fine. How to set tab bar controller into UIView so that I can set another view for some other purpose like iAd?
You just create one tabbar controller in app delegate but don't add it to window, and in view controller you have just add these:
gObjAppDelegate.tabBarController = [[UITabBarController alloc] init];
gObjAppDelegate.tabBarController.viewControllers = #[viewController1, viewController2];
[gObjAppDelegate.tabBarController setSelectedIndex:0];
[self.navigationController pushViewController:gObjAppDelegate.tabBarController animated:YES];
gObjAppDelegate is shared singleton instance of app delegate.This way it worked for me.

Change title of navbar based on which tab is selected?

I have an iOS app where there is a navigation controller as the root controller but at one part there is a tab bar to select between views in one of the nav bars. It looks similar to the iTunes app (navigation bar on top, tab bar on the bottom). I want the title of my navigation bar to change based on which tab is selected. I have two separate controller files for each tab. Here is what I have tried to use in each so far to fix this to no avail:
self.navigationItem.title = #"Title";
self.navigationController.navigationItem.title = #"title";
[self.navigationController setTitle:#"Live"];
[self setTitle:#"Top Title"];
How do I change the NavBar title based on which tab is pressed?
You change the title of the bar in the view controller that is currently being displayed.
So for example, in view controller A that you're showing in the tab controller, you might add:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
[self setTitle:#"A"];
self.tabBarController.navigationItem.title = #"A";
}
Same goes for B, C, etc.
In your ViewControllers that are in the tabs:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.tabBarController.title = self.title;
}
If the individual view controllers presented by the tab bar controller have their own navigation bars, then
[self setTitle:#"Foo"];
will set both the tab bar label, as well as the navigation bar title.
If the navigation controller is at the top level (i.e. the tab bar is inside the navigation controller), then you might have to set the navigation bar title's manually (and you'll want to do this in viewDidAppear rather than viewDidLoad, because these child controllers are not reloaded every time you switch), e.g.:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.navigationController.navigationBar.topItem setTitle:#"Foo"];
}
Alternatively, you could do this navigation bar title adjustment in your UITabBarControllerDelegate method didSelectViewController.
If this doesn't do it, you might have to clarify your question, describing the hierarchy of controllers (e.g. is the tab bar controller inside navigation bar, or vice versa).
You can subclass the UITabBarController, set the delegate to itself, and use the delegate to set its title when the view controller is selected:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
//change the title based on viewController that is selected
self.title = #"New title";
}
Just Two Lines of Code..Only thing is, you need to use viewWillAppear method
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tabBarController.navigationItem.title = #"Your Title";
}
PS: Inspired from Above Answers...
UITabBarController *tabController = (UITabBarController *)self.parentViewController;
tabController.navigationItem.title = #"ABC";
This is working for me
From some R&D on internet
You have to pass the navigationItem up the chain.
The UINavigationController shows the navigationItem belonging to its topViewController which is UITabBarController.
The UITabBarController shows the navigationItem titles in its tabs. So what you need to do is make sure that the tabBarController's navigationItem is it's selectedViewController's navigationItem
So to recap:
UINavigationController title = topViewController.navigationItem.title
UITabBarController tabTitle =
selectedViewController.navigationItem.title
UIViewController title = navigationItem.title

Resources