【问题标题】:How to change the unselected tabbaritem color in iOS7?如何更改 iOS7 中未选中的 tabbaritem 颜色?
【发布时间】:2013-10-29 15:09:24
【问题描述】:

在 iOS 7 之前我用过

[[UITabBar appearance] setTintColor:[UIColor redColor]];

但是现在它只绘制选定的项目,我已经阅读了一些建议,但我不知道该怎么做,我也用这个:

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"openbookwp4.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"openbookwp4.png"]];

这把我想要的图标,我想要的颜色,但只有在我选择了那个选项卡之后,例如,当我打开应用程序时,选项卡看起来很正常,但是在我按下第二个选项卡并返回到第一个选项卡后,第二个标签现在有我想要的颜色。没有图片很难解释,但我不能发布图片......

【问题讨论】:

标签: ios objective-c ios7 uitabbar


【解决方案1】:

此代码适用于 iOS 7。

[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
                                                    NSForegroundColorAttributeName : [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:1]
                                                    } forState:UIControlStateNormal];

根据需要设置前景色。

也影响未选中的标签栏图标:

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], UITextAttributeTextColor, nil]
                                         forState:UIControlStateNormal];

如果它不起作用,唯一的方法是使用选定和未选定状态的图像:

// set selected and unselected icons
UITabBarItem *item = [self.tabBar.items objectAtIndex:0];

// this way, the icon gets rendered as it is (thus, it needs to be green in this example)
item.image = [[UIImage imageNamed:@"unselected-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

// this icon is used for selected tab and it will get tinted as defined in self.tabBar.tintColor
item.selectedImage = [UIImage imageNamed:@"selected-icon.png"];

【讨论】:

  • 你好,这只是改变文字颜色,如何改变图标颜色?谢谢
  • 编辑了我的答案。希望对您有所帮助。
  • 第二个 sn-p 在 iOS7 (UITextAttributeTextColor) 上已弃用,您可以改用 [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor greenColor]} forState:UIControlStateNormal];
  • 谢谢! UIImageRenderingMode.AlwaysOriginal 也适用于 iOS 9。我用 UIAppearance 尝试了许多其他的东西,但除了第一次绘制之外没有任何东西持续存在。这个可以。
  • 为了我的使用,我还必须将渲染模式添加到所选图像。
【解决方案2】:

在我的情况下,问题是我只在 viewDidLoad 中定义了标签栏项目。如果你这样做,很明显,只有在加载相应选项卡的视图后才会设置图像,而不是一开始(当只选择第一个选项卡时)。

我的解决方案是在视图控制器的 init 方法中定义自定义选项卡项,然后即使尚未加载控制器的视图,也可以看到未选择的图像。

【讨论】:

    【解决方案3】:

    从 Nikos Answer 开始

    对于 swift 2.*,它看起来像这样

     UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:.Normal)
    
        UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:.Selected)
    
        let Item1 = self.items![0]
        Item.image = UIImage(named: "Icon1")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    
        let Item2 = self.items![1]
        Item2.image = UIImage(named: "Icon2")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    
        let Item3 = self.items![2]
        Item3.image = UIImage(named: "Icon3")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    

    【讨论】:

      猜你喜欢
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 1970-01-01
      • 2013-10-18
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多