Search code examples
uinavigationcontrollerswift2uitabbarcontrolleruitabbaruitabbaritem

Ios Swift : Displaying Navigation Bar in custom Tabbar


I am trying to create a custom tab bar to place it on top of the screen using below code, programmatically. But the problem is, i am not able to get a navigation bar on top of the tabbar to go back to previous screen. kindly help me.

override func viewDidLoad()
{
    super.viewDidLoad()

    let SrnBrdVar = UIStoryboard(name: "Main", bundle: nil)
    let TabBarCtrVar = UITabBarController()
    let TabBarVar = TabBarCtrVar.tabBar
    let ObxSrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("ObxSrnUid")
    let ObxNavSrnVar = UINavigationController(rootViewController: ObxSrnVar)
    let IbxSrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("IbxSrnUid")
    let IbxNavSrnVar = UINavigationController(rootViewController: IbxSrnVar)

    UIApplication.sharedApplication().delegate!.window?!.rootViewController = TabBarCtrVar

    ObxSrnVar.tabBarItem = UITabBarItem(
        title: "Outbox",
        image: UIImage(named: "NamItmDftImj"),
        selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))
    IbxSrnVar.tabBarItem = UITabBarItem(
        title: "Inbox",
        image: UIImage(named: "NamItmDftImj"),
        selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))

    TabBarCtrVar.viewControllers = [ObxSrnVar,IbxSrnVar]

    TabBarVar.tintColor = UIColor.greenColor()
    TabBarVar.frame = CGRectMake(
        0,
        UIApplication.sharedApplication().statusBarFrame.size.height + 64,
        UIScreen.mainScreen().bounds.width,
        50)
}

Solution

  • This worked for me. Hopefully it will be useful for you as well

     let myStoryBoard = UIStoryboard(name: "Main", bundle: nil)
    
        let myTabBarController = UITabBarController()
    
        let leftController = myStoryBoard.instantiateViewControllerWithIdentifier("left")
    
        let leftNavController = UINavigationController(rootViewController: leftController)
    
        let rightController = myStoryBoard.instantiateViewControllerWithIdentifier("right")
    
        let rightNavController = UINavigationController(rootViewController: rightController)
    
        UIApplication.sharedApplication().delegate!.window?!.rootViewController = myTabBarController
    
        myTabBarController.addChildViewController(leftNavController)
        myTabBarController.addChildViewController(rightNavController)
    
    
    
        let leftChild = myStoryBoard.instantiateViewControllerWithIdentifier("leftChild")
        let rightChild = myStoryBoard.instantiateViewControllerWithIdentifier("rightChild")
    
    
        leftNavController.addChildViewController(leftChild)
        rightNavController.addChildViewController(rightChild)
    
        print(myTabBarController.viewControllers?.count)
       let but1 = myTabBarController.viewControllers?[0].tabBarItem
        but1!.title = "Left Page"
        let but2 = myTabBarController.viewControllers?[1].tabBarItem
            but2!.title = "Right Page"
    
    
        leftNavController.childViewControllers[0].navigationItem.title = "Left View"
        rightNavController.childViewControllers[0].navigationItem.title = "Right View"