xcode swift UITabbaritem 颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26704896/
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-09-15 06:04:47  来源:igfitidea点击:

swift UITabbaritem colors

iosobjective-cxcodeswiftuitabbarcontroller

提问by Marianna

I'm trying to figure out how to use the colors I want for my tabBar.

我试图弄清楚如何使用我想要的颜色tabBar

I know how to change the background, I also know how to change the tabbar.itemcolors and text but I can't understand how to:

我知道如何更改背景,我也知道如何更改tabbar.item颜色和文本,但我不明白如何:

  • the default gray color of the unselected tab bar item
  • to change the color if the item is selected (and I'm using rendering mode always original cause I can't find another way to remove the default gray color from the non selected tab bar item)

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    
        tabBarItem.title = "test"
        tabBarItem.image = UIImage(named: "first.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)   
    
    }
    
  • 未选中的标签栏项目的默认灰色
  • 如果项目被选中,则更改颜色(我使用的渲染模式总是原始的,因为我找不到另一种方法来从非选定的标签栏项目中删除默认的灰色)

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    
        tabBarItem.title = "test"
        tabBarItem.image = UIImage(named: "first.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)   
    
    }
    

how can I use the color I want, in the state I want?

我怎样才能在我想要的状态下使用我想要的颜色?

回答by rickerbh

A UITabBarhas a tintColorproperty, but this sets the tint for the selected image, not the unselected one. You're setting the unselected image correctly AFAIK. For changing the colour of the selected image, you can use the tintColoron the UITabBar(if you want all the images to have the same tint), or set your UITabBarItem's selectedImagewith the rendering mode as AlwaysOriginal.

AUITabBar有一个tintColor属性,但这会为所选图像设置色调,而不是未选择的图像。您正在正确设置未选择的图像 AFAIK。为了改变所选图像的颜色,你可以使用tintColorUITabBar(如果你希望所有的图像具有相同的色调),或将您UITabBarItemselectedImage使用渲染模式AlwaysOriginal

tabBarItem.selectedImage = UIImage(named: "first-selected")!.imageWithRenderingMode(.AlwaysOriginal)

I've set the UIImage to be an unwrapped optional because you probably want it to crash if there is no image file. It'll help ensure your images are actually being loaded, rather than silently failing :-)

我已将 UIImage 设置为未包装的可选,因为如果没有图像文件,您可能希望它崩溃。这将有助于确保您的图像确实被加载,而不是默默地失败:-)

You may also want to set the colour for the label or your text won't match your image colours. The below sets the defaults for all UITabBarItem's, but you can set (or override) it on a per item basis.

您可能还想设置标签的颜色,否则您的文本将与您的图像颜色不匹配。下面为 all 设置默认值UITabBarItem,但您可以在每个项目的基础上设置(或覆盖)它。

UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.blueColor()}, forState:.Selected)
UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.redColor()}, forState:.Normal)

回答by John Riselvato

Here's how you do it in swift 3 / 4

这是您如何快速 3 / 4

  UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blue], for: .selected)
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .normal)

回答by justsee

If you want:

如果你想:

  • a selected tabBarItemto display a colour image
  • an unselected tabBarItemto display a greyed-out image
  • a 选择tabBarItem显示彩色图像
  • 未选中tabBarItem以显示灰色图像

Then you need to ensure the relevant image assets in XCode are set as Render as: Default, then:

然后需要确保XCode中的相关图片资源设置为Render as: Default,那么:

let image = SomeImage
tabBarItem.image = image
tabBarItem.selectedImage = image.withRenderingMode(.alwaysOriginal)

This ensures that for the selectedImagecase you are forcing the image to display as the original, and in any other situation it will render with the expected tints applied.

这确保在selectedImage您强制图像显示为原始图像的情况下,在任何其他情况下,它将使用预期的色调进行渲染。