更改 UITabBar 的色调/背景颜色
UINavigationBar 和 UISearchBar 都有一个 TintColor 属性,允许您更改这两个项目的色调颜色(我知道令人惊讶)。 我想对应用程序中的 UITabBar 执行相同的操作,但现在找到了将其更改为默认黑色的方法。 有任何想法吗?
The UINavigationBar and UISearchBar both have a tintColor property that allows you to change the tint color (surprising, I know) of both of those items. I want to do the same thing to the UITabBar in my application, but have found now way to change it from the default black color. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
我已经能够通过子类化 UITabBarController 并使用私有类来使其工作:
I have been able to make it work by subclassing a UITabBarController and using private classes:
我对最终答案有一个附录。 虽然基本方案是正确的,但使用部分透明颜色的技巧可以改进。 我认为这只是为了让默认渐变显示出来。 哦,还有,TabBar 的高度是 49 像素而不是 48,至少在 OS 3 中是这样。
因此,如果您有一个适当的带有渐变的 1 x 49 图像,那么您应该使用以下 viewDidLoad 版本:
I have an addendum to the final answer. While the essential scheme is correct, the trick of using a partially transparent color can be improved upon. I assume that it's only for letting the default gradient to show through. Oh, also, the height of the TabBar is 49 pixels rather than 48, at least in OS 3.
So, if you have a appropriate 1 x 49 image with a gradient, this is the version of viewDidLoad you should use:
当您只使用 addSubview 时,您的按钮将失去可点击性,因此不要
使用:
When you just use addSubview your buttons will lose clickability, so instead of
use:
没有简单的方法可以做到这一点,您基本上需要子类化 UITabBar 并实现自定义绘图来完成您想要的操作。 为了达到这样的效果,需要做很多工作,但可能是值得的。 我建议向 Apple 提交错误,以便将其添加到未来的 iPhone SDK 中。
There is no simple way to do this, you basically need to subclass UITabBar and implement custom drawing to do what you want. It is quite a bit of work for the effect, but it may be worth it. I recommend filing a bug with Apple to get it added to a future iPhone SDK.
以下是对此的完美解决方案。 这对于我的 iOS5 和 iOS4 来说效果很好。
Following is the perfect solution for this. This works fine with me for iOS5 and iOS4.
在 iOS 7 上:
我还建议首先根据您的视觉需求进行设置:
栏样式在您的视图内容和选项卡栏之间放置了一个微妙的分隔符。
On iOS 7:
I also recommend setting first depending on your visual desires:
The bar style puts a subtle separator between your view content and your tab bar.
[[self tabBar] insertSubview:v atIndex:0];
非常适合我。
[[self tabBar] insertSubview:v atIndex:0];
works perfectly for me.
对我来说,更改选项卡栏的颜色非常简单,例如:-
试试这个!
for me its very simple to change the color of Tabbar like :-
Try this!!!
仅用于背景颜色
或应用程序委托中的
用于更改选项卡栏未选择图标的颜色
对于 iOS 10:
iOS 10 以上:
for just background color
or this in App Delegate
for changing color of unselect icons of tabbar
For iOS 10:
Above iOS 10:
现有答案中有一些好的想法,许多想法略有不同,您的选择还取决于您的目标设备以及您想要实现的外观。
UITabBar
在自定义其外观时是出了名的不直观,但这里还有一些技巧可能会有所帮助:1)。 如果您想去掉光泽覆盖层以获得更平坦的外观,请执行以下操作:
2)。 要将自定义图像设置到 tabBar 按钮,请执行以下操作:
其中
selected
和unselected
是您选择的UIImage
对象。 如果您希望它们是纯色,我发现的最简单的解决方案是创建一个具有所需backgroundColor
的UIView
,然后将其渲染为UIImage 在 QuartzCore 的帮助下。 我在
UIView
的类别中使用以下方法来获取包含视图内容的UIImage
:3) 最后,您可能想要自定义按钮标题的样式。 做:
这可以让你做一些调整,但仍然相当有限。 特别是,您无法自由修改文本在按钮内的放置位置,并且无法为选定/未选定的按钮使用不同的颜色。 如果您想做更具体的文本布局,只需将
UITextAttributeTextColor
设置为清晰,并将文本添加到部分 (code>selected 和unselected
图像中 ( 2)。There are some good ideas in the existing answers, many work slightly differently and what you choose will also depend on which devices you target and what kind of look you're aiming to achieve.
UITabBar
is notoriously unintuitive when it come to customizing its appearance, but here are a few more tricks that may help:1). If you're looking to get rid of the glossy overlay for a more flat look do:
2). To set custom images to the tabBar buttons do something like:
Where
selected
andunselected
areUIImage
objects of your choice. If you'd like them to be a flat colour, the simplest solution I found is to create aUIView
with the desiredbackgroundColor
and then just render it into aUIImage
with the help of QuartzCore. I use the following method in a category onUIView
to get aUIImage
with the view's contents:3) Finally, you may want to customize the styling of the buttons' titles. Do:
This lets you do some adjustments, but still quite limited. Particularly, you cannot freely modify where the text is placed within the button, and cannot have different colours for selected/unselected buttons. If you want to do more specific text layout, just set
UITextAttributeTextColor
to be clear and add your text into theselected
andunselected
images from part (2).另一个解决方案(这是一个 hack)是将 tabBarController 上的 alpha 设置为 0.01,以便它实际上不可见但仍然可点击。 然后在 MainWindow 笔尖底部设置一个 ImageView 控件,并将您的自定义选项卡栏图像设置在 alpha'ed tabBarCOntroller 下方。 然后,当选项卡控制器切换视图时,交换图像、更改颜色或突出显示。
但是,您会失去“...更多”和自定义功能。
Another solution (which is a hack) is to set the alpha on the tabBarController to 0.01 so that it is virtually invisible yet still clickable. Then set a an ImageView control on the bottom of the MainWindow nib with your custom tabbar image underneath the alpha'ed tabBarCOntroller. Then swap the images, change colors or hightlight when the tabbarcontroller switches views.
However, you lose the '...more' and customize functionality.
您好,我正在使用 iOS SDK 4,我只需两行代码就可以解决这个问题,就像这样
希望这会有所帮助!
Hi There am using iOS SDK 4 and i was able to solve this issue with just two lines of code and it's goes like this
Hope this helps!
Swift 3.0 答案:(来自 Vaibhav Gaikwad)
用于更改选项卡栏未选择图标的颜色:
仅用于更改文本颜色:
Swift 3.0 answer: (from Vaibhav Gaikwad)
For changing color of unselect icons of tabbar:
For changing text color only:
Swift 3 使用
AppDelegate
中的外观执行以下操作:UITabBar.appearance().barTintColor = your_color
Swift 3 using appearance from your
AppDelegate
do the following:UITabBar.appearance().barTintColor = your_color
iOS 5 添加了一些新的外观方法来自定义大多数 UI 元素的外观。
您可以使用外观代理来定位应用程序中 UITabBar 的每个实例。
对于 iOS 5 + 6:
对于 iOS 7 及更高版本,请使用以下内容:
使用外观代理将更改整个应用程序中的任何选项卡栏实例。 对于特定实例,使用该类的新属性之一:
iOS 5 has added some new appearance methods for customising the look of most UI elements.
You can target every instance of a UITabBar in your app by using the appearance proxy.
For iOS 5 + 6:
For iOS 7 and above, please use the following:
Using the appearance proxy will change any tab bar instance throughout the app. For a specific instance, use one of the new properties on that class: