setTitleTextAttributes不能与NSForegroundColorAttributeName一起使用

时间:2016-08-01 08:16:34

标签: uitabbarcontroller xcode7 uitabbar uitabbaritem

以下是我在WHERE vw_public_task.complete_date <= @Today AND vw_public_task.complete_date >= @Yesterday

中所做的事情
AppDelegate

这是我的firstViewController:

self.tabBarController.delegate = self;
NSDictionary *unSelectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [Utility primaryColor], NSForegroundColorAttributeName,  nil];
NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [UIColor whiteColor], NSForegroundColorAttributeName, nil];


[[UITabBarItem appearance] setTitleTextAttributes:unSelectedAttributes
                                         forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:selectedAttributes
                                         forState:UIControlStateSelected];

我的标签正在改变颜色,但文字仍为白色和灰色。它没有根据[[UINavigationBar appearance] setBarTintColor:[Utility primaryColor]]; [[UITabBar appearance] setTintColor:[UIColor whiteColor]]; UITabBar * tabBar = self.tabBarController.tabBar; UIImage *tabImage = [UIHelper makeImageWithSizeAndColor:[Utility primaryColor] : CGSizeMake(tabBar.frame.size.width/2, tabBar.frame.size.height)]; tabImage = [tabImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; tabBar.selectionIndicatorImage = [UIHelper makeImageWithSizeAndColor:[Utility primaryColor] : CGSizeMake(tabBar.frame.size.width/tabBar.items.count, tabBar.frame.size.height)]; 更改颜色。

1 个答案:

答案 0 :(得分:0)

尝试使用此代码更改文字颜色。

  [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIColor whiteColor], UITextAttributeTextColor,
                                                           nil] forState:UIControlStateNormal];
        UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];
        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           titleHighlightedColor, UITextAttributeTextColor,
                                                           nil] forState:UIControlStateHighlighted];

好的,不推荐使用..你可以试试这段代码

   [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }
                                             forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor redColor] }
                                             forState:UIControlStateSelected];

我也尝试了你的代码,这也很好。

  NSDictionary *unSelectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIColor blackColor], NSForegroundColorAttributeName,  nil];

    NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIColor redColor], NSForegroundColorAttributeName, nil];


    [[UITabBarItem appearance] setTitleTextAttributes:unSelectedAttributes
                                             forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:selectedAttributes
                                             forState:UIControlStateSelected];