iOS Tutorials: Configure your own TabBar

OVERVIEW

I have to apologize for being inactive for the past few weeks. My wife gave birth and I just couldn’t find the time to update the site weekly. Before we started, let’s take a look at my cute little baby. His name is Lexton Tan and he is now 3 weeks old. Isn’t he adorable. My heart melts everything I see this precious little baby.

Anyway, we are going to be looking at TabBar today. I find myself struggling a lot when I was first learning how to use it and I thought this might help you. Basically, we will be using Tab Bar and will add some Tab Bar Item and with each click, we want it to return something. It’s a very straight forward example but it will definitely help you with your projects.

 

Designing the Storyboard

Let’s go into storyboard and begin designing our own storyboard. Some of the objects that we will be using are:

  1. Tab Bar
  2. Tab Bar Item

With Tab Bar Item, you will have the option to use built-in title/image or you could customized your own by giving your own title and image. Here we will be using the built in and I have picked Recents, Favorites and More. 

Also keep in mind to click on each of the Tab Bar Item and change the tag. You’ll notice that it’s all zero. From the image above, I’ve changed it accordingly. With this, I am telling the program that if the tag is zero, it’s going to show me Recents and the same applies to the rest.

Writing the codes

This the part where took me awhile to understand back in the days as I was using the wrong protocol and couldn’t use my Tab Bar. Here we will be using UITabBarDelegate protocol. Also don’t forget to create an @IBOutlet for your Tab Bar. Once we have all of these down, we could use the func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { . I’ll show you how this is being implemented.

By using UITabBarDelegate, you need to assign it to the Tab Bar outlet which I have just created or else nothing is going to happen. I am actually using switch statement telling the program that if the item tag is 0, show Recent and the rest are self explanatory.

In the end, your simulator should look like mine and should work perfectly fine.

 

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*