如何在iOS 7和iOS 8上正确定制UITabBar和UITabBarItem? [英] How to properly customize UITabBar and UITabBarItem on iOS 7 and iOS 8?
问题描述
我正在谷歌搜索这么多,但我找不到直接和合并答案。
I am googling around so much, but nowhere I find a straight and consolidated answer.
I想要自定义我的 UITabBarController
,以便:
I want to customize myUITabBarController
such that:
-
UITabBar
本身是全黑的 - 项目标题的文字颜色在非突出显示状态下为白色
- 项目标题的文字颜色在突出显示状态中为红色
- 在标签栏中使用多色图标
- the
UITabBar
itself is completely black - the text color of the item titles is white in non-highlighted state
- the text color of the item titles is red in highlighted state
- Use multicolored icons in the tab bar
1。转 UITabBar
黑色
我猜我需要使用 UIAppearance
这个API,实际上我可以使用以下方法转动 UITbarBar
黑色: [[UITabBar外观] setBarTintColor:[UIColor blackColor]] ;
。
1. Turn UITabBar
black
I am guessing I need to use the UIAppearance
API for this, and actually I was able to turn the UITbarBar
black using: [[UITabBar appearance] setBarTintColor:[UIColor blackColor]];
.
然而,在谷歌搜索后,文本项目的颜色似乎没有做我想要的,以下解决方案有意义对我而言,它只会将非突出显示的状态更改为白色,突出显示也会保持白色...
However, the color of the text items doesn't seem to do what I want, after googling around, the following solutions made sense to me, but it only changes the non-highlighted state to white, highlighted stays white as well...
NSDictionary *titleAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
[[UITabBarItem appearance] setTitleTextAttributes:titleAttributes forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor redColor];
NSDictionary *highlightedTitleAttributes = @{NSForegroundColorAttributeName : titleHighlightedColor};
[[UITabBarItem appearance] setTitleTextAttributes:highlightedTitleAttributes forState:UIControlStateHighlighted];
4。多彩多姿的项目
关于五彩图标,到目前为止,只需在 Storyboards 中设置图标就像这样:
4. Multicolored items
About the multicolored icons, so far by approach was to simply set the icons in Storyboards like this:
但是这不符合我的要求,当项目不时,它只显示灰色的整个图标。选择该项目后,图标将完全消失。
But this doesn't do what I want, it only shows the whole icon in grey when the item is not selected. When the item is selected, the icon completely disappears.
这是原始图标:
这是项目的样子未选择 :
This is how it looks when the item is not selected:
这里它处于选定状态,如上所述图标完全消失:
And here it is in the selected stated, as mentioned the icon completely disappears:
所以,我的问题是我能够达到上述要求的准确程度。我目前缺少什么?我最好不要在代码中做任何事情而不是在故事板中吗?
So, my question is how precisely I can achieve the above mentioned requirements. What am I currently missing? Am I better off doing everything in code than in Storyboards?
注意:我的目标是iOS版本大于7.0,所以请包含任何特定于版本的信息iOS 7和iOS 8之间的行为不同。
推荐答案
我遇到了和你一样的问题。据我所知,不同的iOS版本没有区别。
我这样以编程方式解决了它:
I had the same problem as you. As far as I know there is no difference for the different iOS versions.
I solved it programmatically like this:
-
将条形颜色变黑如下(你已经说过了)(在AppDelegate中):
Turning the bar color black works as following (You already said it) (in AppDelegate):
UITabBar.appearance()。 barTintColor = UIColor.blackColor()
要设置不同状态的标题颜色,我使用了这段代码(在AppDelegate):
To set the color of the title for the different states, I used this code (in AppDelegate):
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName!: UIColor.redColor()], forState:.Selected)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName!: UIColor.whiteColor()], forState:.Normal)
(和4.)通过以编程方式设置图像并更改渲染模式,您可以为不同的状态实现不同的项目颜色,多色图标和不同的项目颜色( imageWithRenderingMode:
)来 UIImageRenderingMode.AlwaysOriginal
,如下所示(在所有视图控制器的第一个视图控制器类中执行此操作):
(and 4.) You can achieve the different item colors, multicolored icons and different item colors for the different states, by setting the image programmatically and change the rendering mode (imageWithRenderingMode:
) to UIImageRenderingMode.AlwaysOriginal
, this looks as following (do this in the first view controller class for all your view controllers):
var recentsItem = self.tabBarController!.tabBar.items![0] as UITabBarItem
var recentsItemImage = UIImage(named:"recents.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
var recentsItemImageSelected = UIImage(named: "recentsSelected.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
recentsItem.image = recentsItemImage
recentsItem.selectedImage = recentsItemImageSelected
如果您有任何以下问题,我希望这会有所帮助免费问我。
I hope this helps, if you have any following questions, feel free to ask me.
这篇关于如何在iOS 7和iOS 8上正确定制UITabBar和UITabBarItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!