如何自定义UITabBar - 选择tabbar如何更改所选tabbar的图像

时间:2011-06-13 09:02:38

标签: iphone uitabbarcontroller

如何在选择标签栏时更改图像。帮帮我吧谢谢。

4 个答案:

答案 0 :(得分:1)

您可以在此处找到有关如何创建自定义标签栏

的信息

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

您可以按照以下代码设置图像&图像选择为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

我希望,这可以帮助你很多:)

答案 1 :(得分:1)

您可以使用UITabBarControllerDelegate方法

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

在appDelegate.m文件中使用此代码 并在appDelegate.h文件中添加协议

答案 2 :(得分:0)

您可以制作自定义标签栏:  1.创建标签栏视图控制器  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];

}

在标签栏控制器viewDidLoad中,通过以下方式调用此方法:

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

    [super viewDidLoad];
}

highlightImage中您在传递选择该标签栏项目时显示的图像的位置

答案 3 :(得分:0)

我认为你需要尝试这个,希望这会有所帮助,

我更改了选定的tabbatitem图像,如 -

tabbar控制器委托方法

中的

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

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

通过这个你可以改变你的形象。

或者您可以直接在视图控制器init(或ViewWillAppear)方法中使用,例如

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