Last Updated: March 06, 2019
·
33.7K
· kanshuYokoo

ios customise tabbar with UITabBarController and storyboad scene.

sample of creating own UITabBarController to customise tab bar behaviour on ios. customising not only tint colour of icon or icon itself, add more functionality when switching tab, it required to make your own class inheriting UITabBarController but you want to use the views and tab bar controller scene from story board.

  1. start from tab application default
  2. remove segure from story board connected to views from tab bar scene
  3. set story boad ID to each scene which would be navigated by the tab bar. (FirstView, SecondView for example)
  4. create a new class MyTabBarController
    ```
    class MyTabBarController: UITabBarController, UITabBarControllerDelegate {

    var firstViewController: FirstViewController!
    var secondViewController: SecondViewController!

    override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self
    firstViewController = storyboard?.instantiateViewController(withIdentifier: "FirstView") as? FirstViewController
    secondViewController = storyboard?.instantiateViewController(withIdentifier: "SecondView") as? SecondViewController
    firstViewController.tabBarItem.image = UIImage(named: "customFirstImage")
    secondViewController.tabBarItem.image = UIImage(named: "customFirstImage")

    viewControllers = [firstViewController, secondViewController]
    selectedIndex = 0

    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
    }
    */
    }
    ```

  5. set MyTabBarController as the class of tab bar controller in the story board