-1

I've been able with code-only to add an action to a tab bar item, but I'm more used to coding with the Interface Builder, and am struggling to use what I know about setting up a tab bar controller with an action tab item programmatically to add an action to a tab bar set up in the IB. With a Tab Bar Controller and three View Controllers connected to said TBC in the IB, how would I go about adding an action where when the middle tab bar item is pressed, the View Controller associated would be presented modally? Thanks.

2 Answers2

0

You can only set up associated view controllers in tabbar from the storyboard. You can't add an action to a tabbar item.

When you select a tab in tabbar, the tabBarController will automatically show the associated view controller. To prevent that, you have to implement tabBarController(_ tabBarController: UITabBarController, shouldSelect method of UITabBarControllerDelegate in your tabBarController subclass, where you can decide whether to show the selected tabbar item or not.

Don't forget to set the delegate in your UITabBarController subclass.

extension YourTabBarController: UITabBarControllerDelegate {
    
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    
    if (viewController is 'YourViewControllerClass') {
        // code to present the viewcontroller
        
        return false
    }
    
    return true
}

}
Jithin
  • 913
  • 5
  • 6
  • I have been able to get the above code to work, so long as I've built both the TabBarController and it's associated ViewControllers in code. Is it possible to connect ViewControllers in Storyboard to a TabBarController built programmatically as a work-around? – Papasmurfcfm Aug 16 '20 at 20:29
  • Yes, you can instantiate the view controllers from storyboard and set them to `viewcontrollers` property of the `tabbarController` – Jithin Aug 16 '20 at 20:42
  • see this answer: https://stackoverflow.com/a/28201747/5443937 – Jithin Aug 16 '20 at 20:50
  • After setting up the TabBarController, Xcode wants to set up an initial VC. I do so in the IB, and the only thing the Simulator shows is the VC that is the initial view, with no TabBar controls. – Papasmurfcfm Aug 18 '20 at 01:28
  • did you set your `tabbarController`as the initial screen? – Jithin Aug 18 '20 at 04:37
0

This code Work for me:

import UIKit

class WayTBC: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Tab Bar Customisation
    tabBar.barTintColor = .systemPink
    tabBar.tintColor = .systemTeal
    tabBar.unselectedItemTintColor = .systemGray
    tabBar.isTranslucent = false
    
    viewControllers = [
        createTabBarItem(tabBarTitle: "Search", tabBarImage: "ic_home_search", viewController: SearchVC()),
        createTabBarItem(tabBarTitle: "Tab 2", tabBarImage: "ic_home_search", viewController: SearchVC()),
        createTabBarItem(tabBarTitle: "Tab 3", tabBarImage: "ic_home_search", viewController: SearchVC()),
        createTabBarItem(tabBarTitle: "Tab 4", tabBarImage: "ic_home_search", viewController: SearchVC())
    ]
    
    // Do any additional setup after loading the view.
}

func createTabBarItem(tabBarTitle: String, tabBarImage: String, viewController: UIViewController) -> UINavigationController {
    let navCont = UINavigationController(rootViewController: viewController)
    navCont.tabBarItem.title = tabBarTitle
    navCont.tabBarItem.image = UIImage(named: tabBarImage)
    
    // Nav Bar Customisation
    navCont.navigationBar.barTintColor = .white
    navCont.navigationBar.tintColor = .white
    navCont.navigationBar.isTranslucent = false
    return navCont
}
Shahriar Hossain
  • 119
  • 2
  • 13