gpt4 book ai didi

ios - TabBar选择项更改背景颜色

转载 作者:行者123 更新时间:2023-11-30 11:30:14 25 4
gpt4 key购买 nike

当用户点击选项卡栏项目时,我尝试更改背景颜色。我的代码:

//MARK: func
func settingTabBar(){
//настроили бэкгроунд активного таб бара на зеленный цвет
let numberOfItems = CGFloat((tabBarController?.tabBar.items!.count)!)
let tabBarItemSize = CGSize(width: (tabBarController?.tabBar.frame.width)! / numberOfItems, height: (tabBarController?.tabBar.frame.height)!)
tabBarController?.tabBar.selectionIndicatorImage = UIImage.imageWithColor(color: Helpers.followGreenColor, size: tabBarItemSize).resizableImage(withCapInsets: .zero)

// remove default border
tabBarController?.tabBar.frame.size.width = self.view.frame.width + 4
tabBarController?.tabBar.frame.origin.x = -2
}

选择的项目颜色发生变化但不完全。看下面的屏幕。谢谢。

my screen

my problem

更新:

screen view hierarchy

最佳答案

我用下面的代码解决了这个问题

class ChildTabBarController: UITabBarController {

override func viewDidLoad() {
super.viewDidLoad()

changeBckgColorSelection()
}

fileprivate func changeBckgColorSelection() {
let imgSize = CGSize(width: tabBar.frame.size.width / CGFloat(tabBar.items!.count),
height: tabBar.frame.size.height)
UIGraphicsBeginImageContextWithOptions(imgSize, false, 0)
let p = UIBezierPath(rect: CGRect(x: 0, y: 0, width: imgSize.width,
height: imgSize.height))
Helpers.followGreenColor.setFill()
p.fill()
let finalImg = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.tabBar.selectionIndicatorImage = finalImg
}
}

关于ios - TabBar选择项更改背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50331705/

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