Select 选项卡以编程方式自定义选项卡栏 ios

Select tab programmatically custom tab bar ios

我已使用以下 tutorial 在 ios 中实现自定义选项卡 当我点击任何项目时它工作正常。现在我想以编程方式移动它。例如我收到了 Firebase 的通知,想打开第三个选项卡。但是我没有得到。我在 appdelegate 中引用了 MainTabbarController 实例。 这是我试过的 在CustomTabBar

func customTapped(index :Int){
        animateTabBarSelection(from: selectedTabBarItemIndex, to: index)
        selectedTabBarItemIndex = index
        delegate.didSelectViewController(self, atIndex: index)
}

AppDelegate

mainTabBarController?.customTabBar?.customTapped( index: 2)

根据您随问题提供的link,以下是我发现负责切换选项卡的语句

func barItemTapped(sender : UIButton) {
    let index = tabBarButtons.indexOf(sender)!

    animateTabBarSelection(from: selectedTabBarItemIndex, to: index)
    selectedTabBarItemIndex = index
    delegate.didSelectViewController(self, atIndex: index)
}

您可以将此代码修改为

func barItemTapped(sender : UIButton) {
    let index = tabBarButtons.indexOf(sender)!
    tabSelectedAt(index)

}

func tabSelectedAt(_ index:Int) {
 if selectedTabBarItemIndex != nil {
   animateTabBarSelection(from: selectedTabBarItemIndex, to: index)
  }
    selectedTabBarItemIndex = index
    delegate.didSelectViewController(self, atIndex: index)
}

所以调用函数 func tabSelectedAt(_ index:Int) 与你想要切换的选项卡的索引。