UITabBar 在 iOS7 上更改一个 UITabBarItem 的背景颜色 [英] UITabBar change background color of one UITabBarItem on iOS7

查看:20
本文介绍了UITabBar 在 iOS7 上更改一个 UITabBarItem 的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以更改 UITabBar 中特定 UITabBarItem 的背景颜色吗?

Can I change the background color of a specific UITabBarItem in a UITabBar?

我知道如何更改所选背景的所有背景,使用:

I know how to change all the background of the selected background, using:

[[UITabBar appearance] setBarTintColor:[UIColor redColor]];
[[UITabBar appearance] setTintColor:[UIColor blueColor]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor yellowColor]];

但是可以只对一个 item 做而不需要子类化吗?

But can it be done for only one item without subclassing?

谢谢

推荐答案

您可以在父 tabBar 上添加子视图,并在子视图上设置背景色.你可以使用tabBar框架尺寸来计算你的tabBarItem的偏移量和宽度,然后在下面插入子视图.

You can add a subview to the parent tabBar, and set a background color on the subview. You can use the tabBar frame dimensions to calculate the offset and width of your tabBarItem, and then insert the subview underneath.

示例(在 Swift 中):

Example (in Swift):

// Add background color to middle tabBarItem
let itemIndex = 2
let bgColor = UIColor(red: 0.08, green: 0.726, blue: 0.702, alpha: 1.0)

let itemWidth = tabBar.frame.width / CGFloat(tabBar.items!.count)
let bgView = UIView(frame: CGRectMake(itemWidth * itemIndex, 0, itemWidth, tabBar.frame.height))
bgView.backgroundColor = bgColor
tabBar.insertSubview(bgView, atIndex: 0)

这篇关于UITabBar 在 iOS7 上更改一个 UITabBarItem 的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆