2
votes

I'm facing problem with using UITabBar and UINavigationBar at the same time. My intention is to have tabBar displaying my 3 tabs and navBar on each of these tabs displaying custom name and on some of these tabs additionally displaying some buttons (such as Add button).

I created view controllers in AppDelegate's didFinishLaunchingWithOptions:

AAA *vc1 = [[AAA alloc] init];
BBB *vc2 = [[BBB alloc] init];
CCC *vc3 = [[CCC alloc] init];

Created tabBar and populated it:

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[vc1, vc2, vc3];

Next I have added some titles and images to the tabBarItems of these view controllers which is all working fine, but than I wanted to display navigation bar on top of the application. So I created navigation controller, but I don't know what view controller I need to initiate it with. If I use vc1 and set navController as rootViewController, application displays vc1's view and shows navBar, but doesn't show tabBar.

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc1];
self.window.rootViewController = navController;

//self.window.rootViewController = self.tabBarController;

I tried to set tabBarController to init navController and set navController as rootViewController - in this case views, tabBar and navBAr display correctly, but I don't have navBarItems associated with views vc1, vc2, vc3.

This is how I created navBarItems (e.g. in AAA.m):

- (instancetype)init {
self = [super init];
if (self) {
    UINavigationItem *navItem = self.navigationItem;
    navItem.title = @"name";
}
return self;
}

What do I need to do to make it work all together? Thanks.

EDIT:

I made some modification to the code and now navigation bar is visible, but only on the view controller set in UINavigationController's initWithRootViewController method. For example in following code I can see tab bar on every view and nav bar on the vc2 only. And my intention is to have tab bar and nav bar on every of these three vc.

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc2];

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:vc1, navController, vc3, nil];


self.window.rootViewController = self.tabBarController;
2
you want to show different title on different controller on navigation bar?Dishant Rajput
I want the navigation bar to be visible on all of the tab bar views. I know how to customise it to show different information on different view, but here I can't get it visible using tab bar.rpl

2 Answers

1
votes

Ok, I have an approach for you...for this you need make changes in storyboard. Here is a screenshot enter image description here

Now create a class for UITabBar: class TabBarViewController: UITabBarController

Do not write anything in this class, it just for our reference.

In AppDelegate Class, method: didlaunchFinish:

let vcTabBarViewController = STORYBOARD.instantiateViewController(withIdentifier: "TabBarViewController") as? TabBarViewController
        navigationController.viewControllers = [vcTabBarViewController!]

Now in your first view controller which is on 1st tab, In viewDidLoad method: paste these lines:

let tabBar = self.tabBarController?.tabBar
 let homeTab = tabBar?.items?[0] as UITabBarItem!
 let searchTab = tabBar?.items?[1] as UITabBarItem!
 let myProfileTab = tabBar?.items?[4] as UITabBarItem!

homeTab?.image = UIImage(named: "home-select")!.withRenderingMode(.alwaysOriginal)
homeTab?.image = UIImage(named: "home")!.withRenderingMode(.alwaysTemplate)

searchTab?.image = UIImage(named: "search-select")!.withRenderingMode(.alwaysOriginal)
searchTab?.image = UIImage(named: "search")!.withRenderingMode(.alwaysTemplate)

I hope this will resolve your problem.

1
votes

add these line of code on every controller

[self.tabBarController setTitle:@"xyz"];
self.tabBarController.navigationController.navigationBarHidden=NO;