gpt4 book ai didi

ios - UITabBar 在 iOS7 上更改一个 UITabBarItem 的背景颜色

转载 作者:可可西里 更新时间:2023-11-01 03:22:24 25 4
gpt4 key购买 nike

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

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

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

但是可以只对一个项目完成而不进行子类化吗?

谢谢

最佳答案

可以在父tabBar上添加一个 subview ,并在 subview 上设置背景色。您可以使用 tabBar 框架尺寸来计算 tabBarItem 的偏移量和宽度,然后在下方插入 subview 。

示例(在 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)

关于ios - UITabBar 在 iOS7 上更改一个 UITabBarItem 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20410607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com