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

时间:2017-07-07 05:39:31

标签: ios swift3 custom-controls

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

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

AppDelegate

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

1 个答案:

答案 0 :(得分:1)

从您提供的链接以及问题中,以下是我发现负责切换标签的语句

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)

相关问题