【问题标题】:Change tab bar tint color on iOS 7在 iOS 7 上更改标签栏色调颜色
【发布时间】:2013-09-18 15:51:45
【问题描述】:

有没有办法将 iOS 7 上标签栏的色调从默认的带有蓝色图标的白色更改为带有不同颜色按钮的另一种颜色?

【问题讨论】:

  • 你可能需要继承它。我从未尝试过,所以我不能 100% 确定,但这似乎是一个可能的解决方案

标签: ios ios7 colors tabs


【解决方案1】:

试试下面的:

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

要为非活动按钮着色,请将以下代码放入您的 VC 的viewDidLoad

UITabBarItem *tabBarItem = [yourTabBarController.tabBar.items objectAtIndex:0];

UIImage *unselectedImage = [UIImage imageNamed:@"icon-unselected"];
UIImage *selectedImage = [UIImage imageNamed:@"icon-selected"];

[tabBarItem setImage: [unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem setSelectedImage: selectedImage];

您需要为所有 tabBarItems 执行此操作,是的,我知道这很丑,希望 有更清洁的方法来执行此操作。

斯威夫特:

UITabBar.appearance().tintColor = UIColor.red

tabBarItem.image = UIImage(named: "unselected")?.withRenderingMode(.alwaysOriginal)
tabBarItem.selectedImage = UIImage(named: "selected")?.withRenderingMode(.alwaysOriginal)

【讨论】:

  • 完美。谢谢。还有一个快速的问题,有没有办法为标签栏中的非活动按钮着色(未选中时它们是灰色的)?
  • setFinishedSelectedImage 在 iOS7 中已弃用。
  • @null 非活动按钮的代码具有 forState:UIControlStateNormal ,它会更改我的应用程序中所有选项卡栏项目(选中和未选中)的颜色。还有其他状态 UIControlStateSelected 但没看到非选中。
  • [[UITabBarItem 外观] setTitleTextAttributes.. 没有改变未选中项的颜色,它们保持灰色。
  • 对所有标签栏项使用 UIControlStateNormal,对选中项使用 UIControlStateSelected,例如:[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];跨度>
【解决方案2】:

有一种更简单的方法可以做到这一点。

只需打开文件检查器并选择“全局色调”。

您还可以在 Interface Builder 中设置应用的色调颜色。文件检查器的 Interface Builder Document 部分中的 Global Tint 菜单可让您打开 Colors 窗口或选择特定颜色。

另见:

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TransitionGuide/AppearanceCustomization.html

【讨论】:

  • 全局色调菜单不适用于非故事板笔尖。
  • 这很好,但它在 Xcode 5 中对我不起作用。
  • 不起作用,Xcode 5.1。 “window.tintColor = [UIColor PurpleColor]”可以。
  • Interface Builder Document 的全局色调设置实际上不允许您为应用设置色调。它允许您设置情节提要的色调 - 如果您有多个,您可以混合搭配或在代码中设置它,如上。
  • 多么奇怪。我刚刚尝试在 iOS 7、Xcode 5.1.1 中设置 Storyboard 中的色调颜色,但它对 Storyboard 创建的 UITabBar 没有任何作用。
【解决方案3】:

在应用程序中委托 didFinishLaunchingWithOptions:

window.tintColor = [UIColor purpleColor];

为应用全局设置色调颜色。

【讨论】:

  • 在 Xcode 5.1.1 中的 iOS 7.1 上什么都不做。即使在 AppDelegate 中使用 _window。
【解决方案4】:

iOS 7.1.1

如果有人需要使用全局设置色调颜色:

[[UIView appearance] setTintColor:[UIColor whiteColor]];

didFinishLaunchingWithOptionsAppDelegate 中。

以下代码也将在任何viewDidLoad 方法中仅更改标签栏的色调:

[self.tabBarController.tabBar setTintColor:[UIColor redColor]];

【讨论】:

  • 如果你使用这个选项,你还能逐屏覆盖颜色吗?
  • 使用第二行的代码,您可以分别覆盖每个屏幕的颜色
  • 这是我发现的唯一解决方案,它适用于 iOS 7 中的 VC 的 VDL,用于当前选项卡项。 [self.tabBarController.tabBar setTintColor:[UIColor orangeColor]];
【解决方案5】:

在标签栏的 View Controller 类中写入:

// Generate a black tab bar
self.tabBarController.tabBar.barTintColor = [UIColor blackColor];

// Set the selected icons and text tint color
self.tabBarController.tabBar.tintColor = [UIColor orangeColor];

【讨论】:

    【解决方案6】:

    在尝试了所有建议的解决方案后,我找不到任何有用的解决方案。

    我终于尝试了以下方法:

    [self.tabBar setTintColor:[UIColor orangeColor]];
    

    效果很好。

    我只为每个 TabBarItem 提供了一张图片。甚至不需要 selectedImage。

    我什至在 Child-ViewControllers 中使用它来设置不同的 TintColors:

    UIColor *theColorYouWish = ...;
    if ([[self.parentViewController class] isSubclassOfClass:[UITabBarController class]]){
        UITabBarController *tbc = (UITabBarController *) self.parentViewController;
        [tbc.tabBar setTintColor:theColorYouWish];
    }
    

    【讨论】:

      【解决方案7】:

      您可以将色调颜色和字体设置为 setTitleTextattribute:

      UIFont *font= (kUIScreenHeight>KipadHeight)?[UIFont boldSystemFontOfSize:32.0f]:[UIFont boldSystemFontOfSize:16.0f];
      NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,
                                  tintColorLight, NSForegroundColorAttributeName, nil];
      [[UINavigationBar appearance] setTitleTextAttributes:attributes];
      

      【讨论】:

        【解决方案8】:

        最终对我有用的是:

        [self.tabBar setTintColor:[UIColor redColor]];
        [self.tabBar setBarTintColor:[UIColor yellowColor]];
        

        【讨论】:

          【解决方案9】:

          Interface BuilderTab Bar Controller 的“Attributes Inspector”中,确保您的 Bottom Bar 设置为不透明标签酒吧:

          现在转到您的 AppDelegate.m 文件。查找:

          - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
          

          然后在花括号之间添加这段代码来改变标签栏按钮和标签栏背景的颜色:

          ///----------------SET TAB BAR COLOR------------------------//
          
          //--------------FOR TAB BAR BUTTON COLOR---------------//
          [[UITabBar appearance] setTintColor:[UIColor greenColor]];
          
          //-------------FOR TAB BAR BACKGROUND COLOR------------//
          [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
          

          【讨论】:

            猜你喜欢
            • 2013-09-27
            • 2014-05-05
            • 2013-06-11
            • 1970-01-01
            • 1970-01-01
            • 2015-07-11
            • 2013-09-28
            • 2014-12-23
            • 1970-01-01
            相关资源
            最近更新 更多