sai*_*esh 35 iphone cocoa-touch objective-c uitabbar ios
我需要将UITabBar的选择颜色从默认的蓝色更改为红色.我们如何做到这一点.
Beh*_*dad 56
2017年9月更新: 我写这个答案已经两年了,因为它经常收到upvotes,我应该说这可能是这个问题最糟糕的答案,它容易出错,因为iOS更新可能会破解,很难调试等等,所以请不要做我写的东西,并应用更好的解决方案,如子类化UITabBar或UITabBarController.谢谢.
您可以通过为UITabBar设置"tintColor"属性(键路径)来完成此操作.
这应该做到这一点.您可以根据下面的屏幕截图进行检查.
更多相关: 在UITabBar的Identity Inspector中有一个"Tint"属性,我相信它会做同样的事情,但显然,它什么都不做.它的默认值是选择UITabBarItem时的确切默认填充颜色,所以我的猜测是它将在稳定版本Xcode 7中修复.手指交叉.
Kyl*_*egg 15
在iOS 7中,它只是tintColor.实现此目的的一种方法可能是子类UITabBarViewController,在storyboard中设置自定义类,并在viewDidLoad子类tabBarVC的方法中添加:
[[self tabBar] setTintColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)
Dan*_*iel 11
这非常容易
创建一个UITabBarController的自定义类,并在-(void)viewDidLoad方法中添加以下行:
[[self tabBar] setSelectedImageTintColor:[UIColor greenColor]];
Run Code Online (Sandbox Code Playgroud)
要实现上述结果,请执行以下步骤.
第1步:添加所需的图像Assets.xcassets,并确保它们Render As:Default
第2步:选择您的UITabBar对象并设置Image Tint颜色,此颜色将被选中标签颜色
步骤3:选择UITabBar对象并添加密钥路径:unselectedItemTintColor,在用户定义的运行时属性中键入:Color,值:.Choose color for unselected item
全部完成.
因为在iOS 7中不推荐使用UITextAttributeTextColor,所以您应该使用:
[UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor greenColor]} forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor purpleColor]} forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)
从 iOS 8 开始,它很简单:
UITabBar.appearance().tintColor = UIColor.redColor()
Run Code Online (Sandbox Code Playgroud)
SDK 并没有让这变得容易,但在技术上是可行的。苹果显然认为这是他们一致的外观和感觉愿景的一部分。
UITabBar 是 UIView 的子类。您始终可以继承并实现您自己的-drawRect:
这不是一项简单的任务,但是,您必须从头开始重新实现该类,否则您将面临一些奇怪的副作用的风险。
| 归档时间: |
|
| 查看次数: |
37055 次 |
| 最近记录: |