简体   繁体   中英

How to Customize UITabBar - On selection of tabbar how to change the image of selected tabbar

How to change image on selection of tab bar. Help me out on this Thank you.

You can find here about how to create Custom Tab bar

http://www.rumexit.co.uk/2010/07/how-to-customise-the-tab-bar-uitabbar-in-an-iphone-application-part-1-of-2/

And you can follow the Code below for Setting Images & Image Selected for UIControlStateNormal & UIControlStateSelected

UIImage *btnImage = [UIImage imageNamed:@"Button_Normal.png"];
UIImage *btnImageSelected = [UIImage imageNamed:@"Bouton_Selected.png"];

self.bouton_tab = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
bouton_tab.frame = CGRectMake(xStart, TABYSTART, TABITEMWIDTH, TABITEMHEIGHT); // Set the frame (size and position) of the button)
[bouton_tab setBackgroundImage:btnImage forState:UIControlStateNormal]; // Set the image for the normal state of the button
[bouton_tab setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; // Set the image for the selected state of the button
[bouton_tab setTag:0]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
[bouton_tab setSelected:true]; // Set this button as selected (we will select the others to false as we only want Tab 1 to be selected initially

I hope, this helps you lot:)

You can use UITabBarControllerDelegate method

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
   if(tabBarController.selectedIndex==0)
   {
      [viewController.tabBarItem setImage:[UIImage imageNamed:@"home.png"]];
   }
}

use this code in appDelegate.m file and add a protocol in appDelegate.h file

You can make a custom tab bar: 1. Create tab bar view controller 2. In this VC put this method:

-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |    UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    self.button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [self.button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [self.button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];

    self.button.center = self.tabBar.center;

    [self.view addSubview: self.button];

}

In your tab bar controller viewDidLoad call this method from that way:

- (void)viewDidLoad
{
    [self addCenterButtonWithImage:[UIImage imageNamed:@"bemobile.png"] highlightImage:[UIImage imageNamed:@"bemobileSelected.png"]];

    [super viewDidLoad];
}

Where in highlightImage you pass the image that will be shown when you select that tab bar item

I think you need to try this one, hope this will help,

I have change the selected tabbatitem image like -

in tabbar controller delegate method

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

{
    if([tabBarController selectedIndex] == 0)
    {
        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
    }    
}

through this you can change your image.

Or you can use directly in your view controllers init(or ViewWillAppear) method, like

        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM