gpt4 book ai didi

ios - 如何在 Swift 中选定的 UITabBarItem 图标下方添加小图标

转载 作者:搜寻专家 更新时间:2023-10-31 22:29:26 25 4
gpt4 key购买 nike

我有一个 UITabBarController 子类,我想在选定的 UITabBarItem 图标下方添加一个白色的小矩形图标。我使用了 UIView 并将 TabBarItem 作为 subview 并将 View 作为 subview 添加到它。我在 viewWillAppear 中执行此操作,它会显示,但是当我选择另一个选项卡时,它不会出现在该选项卡栏项目下。这是我的代码:

let view =  orderedTabBarItemViews()[selectedIndex]

bottomIcon.frame = CGRect(x: 0, y: 42, width: 10, height: 3)
bottomIcon.center = CGPoint(x: view.bounds.size.width / 2, y: view.bounds.size.height / 2)
bottomIcon.backgroundColor = UIColor.white
bottomIcon.layer.cornerRadius = 2

view.addSubview(bottomIcon)

orderedTabBarItemViews() 函数获取 TabBarItem 作为 UIView 的数组。这是我要实现的目标的图像

最佳答案

这是我使用 unicode 字符创建的快速 hack ⬬:

extension UITabBarController {

func addDotToTabBarItemWith(index: Int,size: CGFloat,color: UIColor, verticalOffset: CGFloat = 1.0) {

// set distance from tab bar icons
for tabItem in self.viewControllers! {
tabItem.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0.0, vertical: verticalOffset)
}

// set default appearance for tabbar icon title
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: color,NSFontAttributeName:UIFont(name: "American Typewriter", size: size)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: color,NSFontAttributeName:UIFont(name: "American Typewriter", size: size)!], for: .selected)

// place the dot
guard let vc = self.viewControllers?[index] else {
log.error("Couldn't find a TabBar Controller with index:\(index)")
return
}

vc.tabBarItem.title = "⬬"
}
}

关于ios - 如何在 Swift 中选定的 UITabBarItem 图标下方添加小图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41522521/

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