Ios – How to create CustomTabBarController using XIB

customizationiosuitabbar

I am using iOS 5.1 and I need to create a Custom TabBar for my app. When app will be launched, the control will be pushed to the Custom TabbBar Controller class. Here is what I am doing. I have created class derived from UITabBarController and I used XIB also. In the XIB, I can see the UIView Objetc containing the Tab Bar and when I am adding more Tabs in that tabBar, it is not reflecting when I run the App and I can see only One Black TabBar without any title and color. I remember earlier we use the MainWindow.xib to add the TabBarController and for each TabBar Item we add the Navigation Controller so that every tab has its own Navigation controller but I am not getting how it can be done if I have used the XIB derived from UiTabBarController Class.Please let me know if I am unclear at any point. I will give more clarification. All I want to know why the changes on the TabBar of the XIB are not reflecting in the app?

enter image description here

enter image description here

The lower image showing the XIB and App so you can see that I have added the Four Tabs in the UITabBar but in the app there is only one Black Bar.

Best Answer

From the Apple Documentation about UITabBarController: This class is not intended for subclassing. Instead, you use instances of it as-is to present an interface that allows the user to choose between different modes of operation

You can't subclass it. If the original UITabBarController doesn't have the features you are looking for, your only choice is to create a new custom tab bar controller without subclassing the original one.

EDIT: not sure if you want to do this. Are you simply trying to load an UITabBarController from a nib? If so, subclassing is not necessary. It would be better to use storyboards ;-) but you can do it even with nib. You have to simply do this:

1- add an empty xib to the project and call it TabBar (only xib. No source files)

2- put an UITabBarController inside that nib. No other components, only that UITabBarController

3- put this code in the AppDelegate application didFinishLaunchingWithOptions method:

// this code should be already in that method if you have used the empty application model
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// this is the new code to add. You are programmatically loading an instance of UITabBarController from the nib named TabBar.xib
UITabBarController *tb = (UITabBarController *)[[[NSBundle mainBundle]loadNibNamed:@"TabBar" owner:self options:nil] objectAtIndex:0];
// here you are assigning the tb as the root viewcontroller
self.window.rootViewController = tb;

// this code is standard in this method
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;

Then you can customize your UITabBarController and it should load correctly. Take care: if you add other objects inside the nib, you must change the line

UITabBarController *tb = (UITabBarController *)[[[NSBundle mainBundle]loadNibNamed:@"TabBar" owner:self options:nil] objectAtIndex:0];

with a loop and some check to see if you are really loading the UITabBarController and not other components. Now I'm simply loading the first object in the nib, without taking care if it's a UITabBarController or an UIPotato ;-)

Related Topic