0
votes

I am looking at changing the graphics on a tabbar, such as

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
tabBarItem1.title = @"Home";
tabBarItem2.title = @"Maps";
tabBarItem3.title = @"My Plan";
tabBarItem4.title = @"Settings";

The problem I have here is that my tabbarcontroller is not my root view, so how can I reference the tabbarcontroller to change the tab images?

I am following a suggestion from this post (Can I have more than 1 UITabBarController?) that refers to having a tableview that links to one or more tabbarcontrollers.

So my root view is not a tab bar, but the tab bar view is loaded after coming from one previous screen.

I have this all working, with the initial screen and then the tab bar, and everything is working fine, I just need to change the graphics on the tab bar, and am not able to do this as all tutorials on changing tab bar graphics use the app delegate and refer to the tabbarcontroller as the root view.

Any help on this greatly appreciated!

2
The tab bar items belong to the view controller in each tab, so you should change their appearance there, not in the tab bar controller.rdelmar
I am trying to set up the appearance of the tab bar within the app delegate, but am unable to reference the tab bar controller properly in the app delegateR2D2
The point is, that's the wrong way to do it. You should do it in the awakeFromNib or initWithCoder: methods of the individual view controllers.rdelmar
I have since tried to add to the viewdidload method of the individual view controllers, and that seems to be working now.R2D2
viewDidLoad will only be called once you click on a tab for any but the first one, so, that's too late, if you want your custom titles and images to show up as soon as the tab bar controller does.rdelmar

2 Answers

2
votes

Tab bar items belong to the individual content view controllers in each tab, so rather than trying to reference the tab bar controller, you should change the tab item properties in those controllers. If you want those changes to be visible as soon as the tab bar controller comes on screen, you need to put those customizations in their awakeFromNib or initWithCoder methods (for controllers set up in IB).

0
votes

Richard,

Firstly, the post you reference pointed out that this is very bad visual design, but anyway, if you need to do it.

How you reference the tab bar controller depends on how you instantiate it. This stuff should be fairly obvious, so I'm guessing that you're working with xibs or storyboards.

Either way, when you load the second view that is going to have the tab bar on it, you'll be loading a view controller to control the view. Just bind the tab bar to a property of the view controller in IB, and you can do your set up in the view did load of the view controller.

Actually, if you're using IB ( XIB or storyboards), you can just set up your icons there.