Post Page Advertisement [Top]

UITabBar without Storyboard and UITabBarController


Hello Everyone..!, Welcome to this blog.

Now we directly dive into the blog, How to create a Tabbar without storyboard and UITabbarController. TabBar is one kind of Widget in iOS. This contains many amounts of BarItems inside the TabBar. We make this BarItems by using TabBarItem. I show this TabBar Structure as a Tree.

        UITabBar   ---> Parent
              UITabBarItems ---> Child
                      Image
                      Title
                      SelectedImage
                      etc. These are all the properties of the children.

Let's Start coding....!

1.Create a New Project In Xcode



2. Create a file TabBarTest extends of UIViewController





3. Create the TabBar and set it to the ViewController

         var TabBar: UITabBar!
       
         add the TabBar and constraints to the view.


4. Create TabBarItems and Set it to the TabBar

        let tabOne = UITabBarItem(title: "TabOne", image: UIImage(named:"Home"), tag: 0)
        
    let tabTwo = UITabBarItem(title: "Profile", image: UIImage(named:"Profilee"), tag: 1)
        
        TabBar.setItems([tabOne, tabTwo], animated: true)
        
        TabBar.selectedItem = tabOne

   Now run the app, we get the below output.

Simulator Output


    Next, we create two new view controllers to show, after the declare it in our app.
    I create a TabOne and TabTwo VC.

    let tabOneVC = UINavigationController(rootViewController: TabOne())
    let tabTwoVC = UINavigationController(rootViewController: TabTwo())

    I add some BarButtonItems to the NavigationController.



5.Set the Selected tab's ViewController.

    We Insert the Selectedtab's ViewController's View inside the TabBarTest's view.
   
    override func viewWillAppear(_ animated: Bool) {
        
        self.view.insertSubview(tabOneVC.view!, belowSubview: TabBar)
    }




6. Showing the ViewControllers while selecting the TabBarItems.

   set TabBar.delegate = self

   Implement the tabBar didselect method.
   
       func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        switch item.tag {
        case 0:
            self.view.insertSubview(tabOneVC.view!, belowSubview: TabBar)
        case 1:
            self.view.insertSubview(tabTwoVC.view!, belowSubview: TabBar)
        default:
            return
        }
    }

  Now run the app, and get the below type of output, I Hope.





I hope this is helpful to you, If I have any mistakes in the above blog or if you have any doubts please kindly put it in the comment box. 


resources:  TabBar Resources.


Happy Coding....!


2 comments:

Bottom Ad [Post Page]