CosmicMind/Samples

Tab bar no longer loading selected tab's view controller after update

Closed this issue · 2 comments

After a recent update my view controller for a selected tab does not load. I have performed an override on tabBar function in order to determine which tab was selected so that I can change the colors of the tab icons. I discovered that if I comment out the function override then everything works.

Here is the code that worked fine before:

override func tabBar(tabBar: TabBar, willSelect button: UIButton) {
    selectTabItem(tabItem: button as! TabItem)
}

How can I fix this and retain the same functionality as before? I need to know which tabItem was selected so that I can change it's color like this:

func setIconColor(tabItem: TabItem, selected: Bool) {
    if (selected == true) {
        UIView.animate(withDuration: 0.25, animations: {
            tabItem.imageView?.tintColor = Color.grey.darken2
        }, completion: nil)
    } else {
        UIView.animate(withDuration: 0.25, animations: {
            tabItem.imageView?.tintColor = Color.grey.lighten1
        }, completion: nil)
    }
}

So I found a delegate called TabsControllerDelegate, however when I implement func tabsController(tabsController: TabsController, willSelect viewController: UIViewController) it never gets called.

Hey :)

You can access through the TabBar the selectedTabItem when the

optional func tabBar(tabBar: TabBar, didSelect tabItem: TabItem)

is called. You can use the

optional func tabBar(tabBar: TabBar, willSelect tabItem: TabItem)

to change the color of the tabItem back to the original state.

So the tabItem that is currently being transitioned from will be the selectedTabItem property in the willSelect delegate, and when the didSelect delegate is fired, the selectedTabItem will be the one being transitioned to.

If you need, please reopen or create a new issue :)