ios - 自定义 UITabBarController - DidSelect/AnyCustomization 委托问题
问题描述
我创建了自定义Tabbar类,但是我几乎没有问题,例如隐藏了示例选项卡时如何调用此类的任何函数?(我想在标签栏隐藏时隐藏我的条纹)另外,当我使用tabBarController?.selectedIndex = 3
委托时 didSelect 没有被调用,我该如何解决?那是我的简单代码。谢谢你的帮助
class customTabBar: UITabBarController {
var stripe = UIView()
override func viewDidLoad() {
super.viewDidLoad()
let cellWidth = tabBar.frame.width/5
stripe = UIView(frame: CGRect(x: 0, y: tabBar.frame.minY + 20, width: (tabBar.frame.width/5) * 0.6, height: 6))
stripe.center.x = cellWidth/2
stripe.applyGradient(colours: [UIColor.init(hexFromString: "5897ee"),UIColor.init(hexFromString: "5228d8")])
stripe.layer.cornerRadius = 2
stripe.layer.masksToBounds = true
self.view.addSubview(stripe)
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let index: Int = item.tag + 1
let cellWidth = tabBar.frame.width/5
let newPostion = cellWidth * CGFloat(index)
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.3, options: .curveEaseInOut, animations: {
self.stripe.center.x = newPostion - (cellWidth/2)
})
}
}
解决方案
class MyTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
//Add the stripe to tabBar,so it will hidden when tabBar hidden
let cellWidth = tabBar.frame.width/5
let stripe = UIView(frame: CGRect(x: 0, y: tabBar.frame.minY + 20, width: (tabBar.frame.width/5) * 0.6, height: 6))
stripe.center.x = cellWidth/2
stripe.layer.cornerRadius = 2
stripe.layer.masksToBounds = true
self.tabBar.addSubview(stripe)
}
//use this to observe tabBarController?.selectedIndex = 3
override var selectedIndex: Int{
didSet{
//do what you want
}
}
}
extension UIViewController {
//use this to call get MyTabBarController instance and call any function
var myTabBarcontroller : MyTabBarController?{
get{
return self.tabBarController as? MyTabBarController
}
}
}
推荐阅读
- angular - 单元测试 Observable 订阅中抛出的错误
- javascript - 当组织 npm 中有多个可用版本时会发生什么
- javascript - 如何使用 Python 和 Beautiful-soup 从 Instagram 抓取标签
- python - Python group by 并找到匹配条件的第一个序列
- c++ - `static` 的使用如何影响我的代码速度?
- git - 为什么当我输入命令“git reflog”时 Git Bash 什么也不显示
- laravel - Laravel Sanctum 用于制作 Api
- powershell - 在 Powershell 中使用自定义文件列表(文件路径和大小)填充 ListView
- python - R的Keras:AttributeError:'Tensor'对象没有属性'_numpy'
- javascript - 如何将firebase中的数据保存在变量上