Manthan
Manthan

Reputation: 3914

set tabbar appearance different for different view controllers in ios

I am having a Tab bar based application.

I have set the background image for Tab bar in my applicationDidFinishLaunchingWithOptions with the code shown below.

UIImage* tabBarBackground = [UIImage imageNamed:@"tabbarbottom2.png"];
        [[UITabBar appearance] setBackgroundImage:tabBarBackground];

I am doing this in a storyboard.

Now, with this code. My Tabbar's background image is getting set and working well.

But I want to set different background for different viewcontrollers of tabbar.

I have searched a lot but couldn't find a way to do so.

I am stuck on this for quite a while.

Please help me on this.

Thanks in advance.

Upvotes: 0

Views: 335

Answers (2)

davetw12
davetw12

Reputation: 1843

It's best practice to set your tabBar appearance in viewDidLoad method of each viewController. That way that the appearance change appears more natural, rather than the tabBar changing appearance after it has already been show to the user as it would if you did this in the viewDidAppear. You can accomplish this with:

- (void)viewDidLoad:(BOOL)animated {
    UIImage* tabBarBackground = [UIImage imageNamed:@"tabbarbottom2.png"];
    [[UITabBar appearance] setBackgroundColor:tabBarBackground];
}    

Upvotes: 1

Lev Landau
Lev Landau

Reputation: 808

You can always override the default in viewDidLoad. If you want to set the background image for different tab bar controllers you would have to subclass UITabBarController and add the following to viewDidLoad.

[self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabbarForThisController.png"]];

If you want different pictures depending on the selected tab you have to add

[self.tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"tabBarForThisTab.png"]];

to viewDidLoad in the view controller for the selected tab.

Upvotes: 1

Related Questions