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

查看:104
本文介绍了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]];

但是只能在没有子类化的情况下对一个项目进行吗?

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天全站免登陆