如何选择自定义标签栏项目并选择未选中的透明标签栏项目

时间:2012-11-17 20:56:58

标签: iphone ios5 uitabbarcontroller tabbar uitabbaritem

我有两个自定义标签栏项目,我希望在某个控制器中选择它们时会以彩色突出显示。但是,当它们未被选中时,我希望tabbar显示我创建的透明标签栏项目图像。

我尝试在视图控制器中实现相应的代码,但它无法正常工作。

[[self tabBarItem] setFinishedSelectedImage:[UIImage imageNamed:@"tab.png"] `withFinishedUnselectedImage:[UIImage imageNamed:@"transparent.png"]];`
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil方法

中的

我做错了什么?

1 个答案:

答案 0 :(得分:0)

使用以下方法自定义标签栏:

//UITabBar Customizing Appearance
{
    [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabBarBg"]];

    int numberOfTabs = 0;
    numberOfTabs = [[self.tabBarController viewControllers] count];

    UIImage *selectionIndicatorImage = [UIImage imageNamed:@"selectionIndicatorImage"];
    if (numberOfTabs) {
        selectionIndicatorImage = [AppDelegate scaleImage:selectionIndicatorImage
                                                   toSize:CGSizeMake((320.0/numberOfTabs), selectionIndicatorImage.size.height)];
    }

    [[UITabBar appearance] setSelectionIndicatorImage:selectionIndicatorImage];        
    [[UITabBar appearance] setSelectedImageTintColor:[UIColor colorWithRed:0.0 green:169.0/255.0 blue:157.0/255.0 alpha:1.0]];

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor colorWithWhite:1.0 alpha:1], UITextAttributeTextColor,
                                                       [UIColor lightGrayColor], UITextAttributeTextShadowColor, nil]
                                             forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor colorWithWhite:1.0 alpha:1], UITextAttributeTextColor,
                                                       [UIColor lightGrayColor], UITextAttributeTextShadowColor, nil]
                                             forState:UIControlStateSelected];
}

使用创建的图像作为所选和未选定标签项的图像。还可以指定标签的数量。