ios 更改故事板中的标签栏项目选定颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26835148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:34:01  来源:igfitidea点击:

Change tab bar item selected color in a storyboard

iosxcodexcode6uitabbaruitabbaritem

提问by Deekor

I want to change my tab bar items to be pink when selected instead of the default blue.

我想在选择时将我的标签栏项目更改为粉红色,而不是默认的蓝色。

How can i accomplish this using the storyboard editor in Xcode 6?

如何使用 Xcode 6 中的故事板编辑器完成此操作?

Here are my current setting which are not working, the blue background works but the pink doesnt work:

这是我当前无法使用的设置,蓝色背景有效,但粉红色背景无效:

enter image description here

在此处输入图片说明

回答by Mehul Thakkar

Add Runtime Color attribute named "tintColor" from StoryBoard. This is working(for Xcode 8 and above).

从 StoryBoard 添加名为“tintColor”的运行时颜色属性。这是有效的(对于 Xcode 8 及更高版本)。

if you want unselected color.. you can add unselectedItemTintColortoo.

如果你想要未选择的颜色..你也可以添加unselectedItemTintColor

setting tintColor as Runtime Attribute

将 tintColor 设置为运行时属性

回答by Marcelo Gracietti

This elegant solution works great on SWIFT 3.0, SWIFT 4.2and SWIFT 5.1:

这个优雅的解决方案在SWIFT 3.0SWIFT 4.2SWIFT 5.1上效果很好:

On the Storyboard:

在故事板上:

  1. Select your Tab Bar
  2. Set a Runtime Attibutecalled tintColorfor the desired color of the Selected Iconon the tab bar
  3. Set a Runtime Attibutecalled unselectedItemTintColorfor the desired color of the Unselected Iconon the tab bar
  1. 选择您的标签栏
  2. 为选项卡栏上选定图标的所需颜色设置名为tintColor运行时属性
  3. 设置名为unselectedItemTintColor运行时属性,用于选项卡栏上未选择图标的所需颜色

enter image description here

在此处输入图片说明

Edit: Working with Xcode 8/10, for iOS 10/12 and above.

编辑:使用 Xcode 8/10,适用于 iOS 10/12 及更高版本

回答by Jarrod

In Swift, using xcode 7 (and later), you can add the following to your AppDelegate.swift file:

在 Swift 中,使用 xcode 7(及更高版本),您可以将以下内容添加到 AppDelegate.swift 文件中:

UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)

This is the what the complete method looks like:

这是完整方法的样子:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // I added this line
    UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)

    return true
}

In the example above my item will be white. The "/255.0" is needed because it expects a value from 0 to 1. For white, I could have just used 1. But for other color you'll probably be using RGB values.

在上面的例子中,我的项目是白色的。“/255.0”是必需的,因为它需要一个从 0 到 1 的值。对于白色,我可以只使用 1。但是对于其他颜色,您可能会使用 RGB 值。

回答by Francisco Romero

On Xcode8I have changed the ImageTintfrom the storyboard and it works well.

Xcode8我已经改变了ImageTint故事板,它运行良好。

enter image description here

在此处输入图片说明

The result:

结果:

enter image description here

在此处输入图片说明

回答by Derek Soike

Swift 3 | Xcode 10

斯威夫特 3 | Xcode 10

If you want to make all tab bar items the same color(selected & unselected)...

如果您想让所有标签栏项目具有相同的颜色(选中和未选中)...



Step 1

第1步

Make sure your image assets are setup to Render As = Template Image. This allows them to inherit color.

确保您的图像资产设置为Render As = Template Image。这允许他们继承颜色。

Xcode Assets

Xcode 资产



Step 2

第2步

Use the storyboard editor to change your tab bar settings as follows:

使用情节提要编辑器更改标签栏设置,如下所示:

  • Set Tab Bar: Image Tintto the color you want the selected icon to inherit.
  • Set Tab Bar: Bar Tintto the color you want the tab bar to be.
  • Set View: Tintto the color you want to see in the storyboard editor, this doesn't affect the icon color when your app is run.
  • Tab Bar: Image Tint设置为您希望所选图标继承的颜色。
  • Tab Bar: Bar Tint设置为您希望标签栏的颜色。
  • View: Tint设置为您希望在故事板编辑器中看到的颜色,这不会影响您的应用程序运行时的图标颜色。

Xcode Storyboard Editor

Xcode 故事板编辑器



Step 3

第 3 步

Steps 1 & 2 will change the color for the selected icon. If you still want to change the color of the unselected items, you need to do it in code. I haven't found a way to do it via the storyboard editor.

步骤 1 和 2 将更改所选图标的颜色。如果你还想改变未选中项的颜色,则需要在代码中进行。我还没有找到通过故事板编辑器来做到这一点的方法。

Create a custom tab bar controller class...

创建自定义标签栏控制器类...

//  TabBarController.swift

class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // make unselected icons white
        self.tabBar.unselectedItemTintColor = UIColor.white
    }
}

... and assign the custom class to your tab bar scene controller.

...并将自定义类分配给您的标签栏场景控制器。

Xcode Storyboard Editor

Xcode 故事板编辑器



If you figure out how to change the unselected icon color via the storyboard editor please let me know. Thanks!

如果您想知道如何通过故事板编辑器更改未选择的图标颜色,请告诉我。谢谢!

回答by Nick

put this code in the viewDidLoad of the view controller that you want to change the color of

将此代码放在要更改颜色的视图控制器的 viewDidLoad 中

[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];

回答by ComboPrime

XCode 8.2, iOS 10, Swift 3: now there's an unselectedItemTintColorattribute for tabBar:

XCode 8.2、iOS 10、Swift 3:现在有一个unselectedItemTintColor属性tabBar

self.tabBar.unselectedItemTintColor = UIColor(red: 0/255.0, green: 200/255.0, blue: 0/255.0, alpha: 1.0)

回答by Arvind Kumar

You can also set selected image bar tint color by key path:

您还可以通过关键路径设置选定的图像条色调颜色:

enter image description here

在此处输入图片说明

Hope this will help you!! Thanks

希望能帮到你!!谢谢

回答by Genevios

You can change colors UITabBarItem by storyboard but if you want to change colors by code it's very easy:

您可以通过情节提要更改 UITabBarItem 的颜色,但如果您想通过代码更改颜色,则非常简单:

// Use this for change color of selected bar

// 使用它来改变选定条的颜色

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

// This for change unselected bar (iOS 10)

// 这用于更改未选中的栏(iOS 10)

   [[UITabBar appearance] setUnselectedItemTintColor:[UIColor yellowColor]];

// And this line for change color of all tabbar

// 此行用于更改所有标签栏的颜色

   [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];

回答by SHINTO JOSEPH

Add this code in your app delegate -did_finish_launching_with_options function

将此代码添加到您的应用程序委托 -did_finish_launching_with_options 函数中

UITabBar.appearance().tintColor = UIColor( red: CGFloat(255/255.0), green: CGFloat(99/255.0), blue: CGFloat(95/255.0), alpha: CGFloat(1.0) )

put the RGB of the required color

放入所需颜色的RGB