2

I am new to Swift. How can I show the tab bar in all view controllers in swift 3 programmatically without using storyboard ?

The scenario is like as follows

1.I have 3 view controllers(e.g view1, view2, view3) attached with tab bar

2.When I clicked a button in side the view2 it navigates to another view controller(lets say view4) and in view4 the tab bar not appears

I have added the tab bar in a view controller(lets say homeView)

This is my code in side the viewDidLoad() of homeView

// Create Tab one
      let homeviewController = view1()
      let homeTabBarItem = UITabBarItem(title: nil, image: UIImage(named: "iconHome"), selectedImage: UIImage(named: "iconHome@3x"))
      homeviewController.tabBarItem = homeTabBarItem
      homeviewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0)

    // Create Tab two
    let categoryviewController = view2()
    let categoryTabBarItem = UITabBarItem(title: nil, image: UIImage(named: "iconBrowse"), selectedImage: UIImage(named: "iconBrowse@3x"))
    categoryviewController.tabBarItem = categoryTabBarItem
    categoryviewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0)

    // Create Tab three
    let userprofviewController = view3()
    let userTabBarItem = UITabBarItem(title: nil, image: UIImage(named: "iconProfile@3x"), selectedImage: UIImage(named: "iconProfile"))
    userprofviewController.tabBarItem = userTabBarItem
    userprofviewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0)


    UITabBar.appearance().barTintColor = UIColor.black
    UITabBar.appearance().itemPositioning = .fill
    self.viewControllers = [homeviewController, categoryviewController, userprofviewController]

Thanks in advance

2 Answers2

2

Instead of taking viewControllers directly to self.viewControllers, Take UINavigationController as follows,

    // Create Tab one
    let homeviewController = view1()
    let homeTabBarItem = UITabBarItem(title: nil, image: UIImage(named: "iconHome"), selectedImage: UIImage(named: "iconHome@3x"))
    homeviewController.tabBarItem = homeTabBarItem
    homeviewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0)
    let navHome = UINavigationController.init(rootViewController: homeviewController)

    // Create Tab two
    let categoryviewController = view2()
    let categoryTabBarItem = UITabBarItem(title: nil, image: UIImage(named: "iconBrowse"), selectedImage: UIImage(named: "iconBrowse@3x"))
    categoryviewController.tabBarItem = categoryTabBarItem
    categoryviewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0)
    let navCategory = UINavigationController.init(rootViewController: categoryviewController)

    // Create Tab three
    let userprofviewController = view3()
    let userTabBarItem = UITabBarItem(title: nil, image: UIImage(named: "iconProfile@3x"), selectedImage: UIImage(named: "iconProfile"))
    userprofviewController.tabBarItem = userTabBarItem
    userprofviewController.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0)
    let navUserPrfl = UINavigationController.init(rootViewController: userprofviewController)


    UITabBar.appearance().barTintColor = UIColor.black
    UITabBar.appearance().itemPositioning = .fill
    self.viewControllers = [navHome, navCategory, navUserPrfl]
  • Thanks Manikanta for your valuable reply, but by taking UINavigationController as you mentioned the following error comes : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported' – Rama Mahapatra Jul 13 '17 at 15:15
  • Hi Rama Mahapatra, Please share your code where you are initialising Tabbar. – Manikanta Chintapalli Jul 14 '17 at 06:45
0

You can try below

class CustomTabBarController: UITabBarController,UITabBarControllerDelegate {

    override func viewDidLoad() 
    { 
        super.viewDidLoad()
        self.delegate = self

        viewControllers = [  setViewController( “Tab 1”, imageName: “ic_tab1”), setViewController( "Tab 2”, imageName: “ic_Tab2”) ,   setViewController( "Tab 3”, imageName: "ic_Tab3”)  , setViewController("Tab 4”, imageName: “ic_Tab4”)]
    }

    fileprivate func createDummyNavControllerWithTitle(_ title: String, imageName: String) -> UINavigationController {
        let viewController = UIViewController()
        let navController = UINavigationController(rootViewController: viewController)
        navController.tabBarItem.title = title
        navController.tabBarItem.image = UIImage(named: imageName)
        return navController
    }
    fileprivate func setViewController( _ title: String, imageName: String) -> UINavigationController {
        let layout = UICollectionViewFlowLayout()
        let list =  ListController(collectionViewLayout: layout)
        list.navtTitle = title
        let navController = UINavigationController(rootViewController: list)
          navController.tabBarItem.title = title
        list.isContactsScreen = true
         navController.tabBarItem.image = UIImage(named: imageName)
        return recentMessagesNavController
    }

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

       // print("Selected item")
    }

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
      //  print("Selected view controller")
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Thanks Rahul for the reply. But ListController function and recentMessagesNavController are undefined – Rama Mahapatra Jul 13 '17 at 11:54
  • ListController should be your viewcontroller which you want to load (in my case i was loading collectionview showing list so it was named listcontroller) and instead of recentMessagesNavController it should return navController – Rahul Kasar Jul 14 '17 at 17:53