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

1

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

2 Comments

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'
Hi Rama Mahapatra, Please share your code where you are initialising Tabbar.
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")
    }
}

2 Comments

Thanks Rahul for the reply. But ListController function and recentMessagesNavController are undefined
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.