ios 修改UITabBarItem字体颜色,正常与选中状态

来源:互联网 发布:淘宝店铺名称能修改吗 编辑:程序博客网 时间:2024/04/28 01:48

言归正传,设置UITabBarItem使用自己设计的图片,其中UITabBarItem中有一个方法

[plain] view plaincopy
  1. - (void)setFinishedSelectedImage:(UIImage *)selectedImage withFinishedUnselectedImage:(UIImage *)unselectedImage   

第一个参数是选中的时候的图片,第二个参数是未选中的时候的图片。


另外,UITabBarItem自带的文字未选中时是白色的,我们想要修改这个字体颜色,怎么办呢?

UITabBarItem的父类UIBarItem提供了这样一个方法

[plain] view plaincopy
  1. - (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;  
说明该方法是支持UIAppearance协议的。这个方法怎么用呢。

下面举个例子

[plain] view plaincopy
  1. [tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:  
  2.                                           [UIColor whiteColor], UITextAttributeTextColor,  
  3.                                           nil] forState:UIControlStateNormal]; 
  4. //修改tabar的选中背景

     self.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"team_img.png"];



     UIImage *tabBarItemImage =nil;

       UIImage *tabBarItemSeletedImage = nil;

       UITabBarItem *tabBarItem = nil;

       UIColor *colorSelect=[[UIColoralloc]initWithRed:0.54green:0.048blue:0.284 alpha:1];

         UIColor *color=[[UIColoralloc]initWithRed:0.23green:0.036blue:0.235alpha:1];

        

        //tabBar的成是

        HomePag *memberCrl = [[HomePagalloc]initWithTabTitle:@"www.fanmoney.cn"TabbarHidden:YES];

        tabBarItemImage = [UIImageimageNamed:@"nav_selection.png"];

        tabBarItemSeletedImage=[UIImageimageNamed:@"nav_selection_active.png"];

        tabBarItem = [[UITabBarItemalloc] initWithTitle:@"首页"image:tabBarItemImage tag:0];

        memberCrl.tabBarItem = tabBarItem;

        [memberCrl.tabBarItemsetFinishedSelectedImage:tabBarItemSeletedImagewithFinishedUnselectedImage:tabBarItemImage];

        UINavigationController *navMember = [[UINavigationControlleralloc] initWithRootViewController:memberCrl];

        [tabBarItem setTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:

                                            color,UITextAttributeTextColor,

                                           nil] forState:UIControlStateNormal];

        [tabBarItem setTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:

                                            colorSelect,UITextAttributeTextColor,

                                           nil] forState:UIControlStateSelected];