【问题标题】:Custom UITabBarController - DidSelect/AnyCustomization delegate problem自定义 UITabBarController - DidSelect/AnyCustomization 委托问题
【发布时间】:2020-02-09 15:09:24
【问题描述】:

我创建了自定义 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)

    })
}

}

【问题讨论】:

    标签: ios swift uitabbarcontroller tabbar


    【解决方案1】:
    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
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-17
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 2016-11-08
      相关资源
      最近更新 更多