xcode 在 UITabBar 中为所选选项卡设置色调颜色

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

setting tint color for selected tab in UITabBar

iosiphoneobjective-cxcodeuitabbar

提问by codercat

In Xcode 5 Dev Preview 2, I was able to execute the following:

在 Xcode 5 Dev Preview 2 中,我能够执行以下操作:

[[UITabBar appearance] setTintColor:[UIColor whiteColor]];//the color of the selected image and text (white)

[[UITabBar appearance] setTintColor:[UIColor whiteColor]];//被选中的图片和文字的颜色(白色)

In Xcode 5 Dev Preview 3, the same line of code throws an exception (see below). The exception indicates that I may want to use 'barTintColor' - but I do not - as this is the color of the overall UITabBar. How can I set the color of the selected image and text in a UITabBar?

在 Xcode 5 Dev Preview 3 中,同一行代码抛出异常(见下文)。异常表明我可能想要使用 ' barTintColor' - 但我没有 - 因为这是整个 UITabBar 的颜色。如何在 UITabBar 中设置所选图像和文本的颜色?

The new exception in Xcode 5 Dev Preview 3:

Xcode 5 Dev Preview 3 中的新异常:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-setTintColor: is not allowed for use with the appearance proxy. Perhaps you want to use the barTintColor property.'

Thank you

谢谢

回答by Josh Brown

I'm not seeing this with the latest Xcode 5 (5.0.2), but I do know that you want to call different methods to set the selected image tint color depending on whether you're running on iOS 6 or 7. Here's some sample code from one of my apps:

我在最新的 Xcode 5 (5.0.2) 中没有看到这一点,但我知道您想调用不同的方法来设置所选图像的色调颜色,具体取决于您是在 iOS 6 还是 7 上运行。这里有一些来自我的一个应用程序的示例代码:

if ([RFSUtilities isIOS7OrHigher])
{
    [[UITabBar appearance] setTintColor:[UIColor whiteColor]];
}
else
{
    [[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
}

The +[RFSUtilities isIOS7OrHigher]just checks to see whether we're running on iOS 7 or above with the proper version check:

+[RFSUtilities isIOS7OrHigher]刚刚检查,看看我们在iOS 7或以上,是否正在运行正确的版本检查

+ (BOOL)isIOS7OrHigher
{
    float versionNumber = floor(NSFoundationVersionNumber);
    return versionNumber > NSFoundationVersionNumber_iOS_6_1;
}

Hope this helps!

希望这可以帮助!

回答by DazChong

Just add tintColorwith color type in User Defined Runtime Attribute of the UITabBarclass.

只需在tintColor类的用户定义运行时属性中添加颜色类型即可UITabBar

Still looking for the similar approach for inactive tabs... Anyone know?

仍在寻找非活动标签的类似方法......有人知道吗?