简体   繁体   English

如何自定义 UITabBar - 在选择标签栏时如何更改所选标签栏的图像

[英]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/ 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您可以按照下面的代码为 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您可以使用 UITabBarControllerDelegate 方法

-(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在 appDelegate.m 文件中使用此代码并在 appDelegate.h 文件中添加协议

You can make a custom tab bar: 1. Create tab bar view controller 2. In this VC put this method:您可以制作自定义标签栏: 1. 创建标签栏视图 controller 2. 在此 VC 中放置此方法:

-(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:在您的标签栏中 controller viewDidLoad从这种方式调用此方法:

- (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 itemhighlightImage中,您传递 select 该选项卡栏项时将显示的图像

I think you need to try this one, hope this will help,我认为你需要尝试这个,希望这会有所帮助,

I have change the selected tabbatitem image like -我已经更改了选定的 tabbatitem 图像,例如 -

in tabbar controller delegate method在标签栏中 controller 委托方法

- (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或者您可以直接在视图控制器中使用 init(或 ViewWillAppear)方法,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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