How to change tab bar item ViewController on basis of some condition?

Recently, I had some trouble figuring out how to make it so my ViewControllers in tab bar changes based on some condition (In my case, whether user is subscribed or not.)

First I was looking if I could change ViewController on click of tab bar item but wasn’t able to find any answers. (Note: I had my tab bar in storyBoard)

Later if found my answer, That was to make my tab bar programmatically and change tab bar item ViewConterollers on bases of condition. I don’t know if it’s the best implementation or not but it works. To change my tab bar I used local Notifications.

Here is my code to make tabbarProgramatically:

In my TabbarController class, InviewWillAppear :

Class myTabbar: UITabbarController{

**var** navHome:UINavigationController?

**var** navMessage:UINavigationController?

**var** navfavourite:UINavigationController?

**var** navviewedBy:UINavigationController?

**var** navUserProfile:UINavigationController?

**var** viewArray = [UINavigationController?]()

**overridefunc** viewWillAppear( **_** animated: Bool) {

setUpTabBar()

}

**func** setUpTabBar(){

**let** homeVC = storyboard?.instantiateViewController(withIdentifier: "HomeVC") **as** ! HomeVC

navHome = UINavigationController.init(rootViewController: homeVC)

navHome!.navigationBar.isHidden = **true**

**let** messageVC = storyboard?.instantiateViewController(withIdentifier: "MessagesVC") **as** ! MessagesVC

navMessage = UINavigationController.init(rootViewController: messageVC)

navMessage!.navigationBar.isHidden = **true**

**let** favouriteVC = storyboard?.instantiateViewController(withIdentifier: "FavouriteVC") **as** ! FavouriteVC

navfavourite = UINavigationController.init(rootViewController: favouriteVC)

navfavourite!.navigationBar.isHidden = **true**

**let** viewedByVC = storyboard?.instantiateViewController(withIdentifier: "ViewedByVC") **as** ! ViewedByVC

navviewedBy = UINavigationController.init(rootViewController: viewedByVC)

navviewedBy!.navigationBar.isHidden = **true**

**let** userProfileVC = storyboard?.instantiateViewController(withIdentifier: "UserProfileVC") **as** ! UserProfileVC

navUserProfile = UINavigationController.init(rootViewController: userProfileVC)

navUserProfile!.navigationBar.isHidden = **true**

viewArray = [navHome , navMessage , navfavourite , navviewedBy , navUserProfile]

**self** .viewControllers = viewArray **as** ? [UIViewController]

tabBar.items![0].image = UIImage(named: "HomeTab")

tabBar.items![1].image = UIImage(named: "ChatTab")

tabBar.items![2].image = UIImage(named: "FavTab")

tabBar.items![3].image = UIImage(named: "ViewTab")

tabBar.items![4].image = UIImage(named: "ProfileTab")

tabBar.items![0].imageInsets = UIEdgeInsets.init(top: 5,left: 0,bottom: -5,right: 0)

tabBar.items![1].imageInsets = UIEdgeInsets.init(top: 5,left: 0,bottom: -5,right: 0)

tabBar.items![2].imageInsets = UIEdgeInsets.init(top: 5,left: 0,bottom: -5,right: 0)

tabBar.items![3].imageInsets = UIEdgeInsets.init(top: 5,left: 0,bottom: -5,right: 0)

tabBar.items![4].imageInsets = UIEdgeInsets.init(top: 5,left: 0,bottom: -5,right: 0)

}

}

Hi, @AppsMaven, I have formatted your code for you to make it easier for us to read. In the future, when you post code here you can format it yourself by placing three backticks(```) on the line before the code, and three backticks on the line after the code. Alternatively, you can highlight your pasted code and select the <\> icon from the top of the edit area of your post.

1 Like

For sure @WebMachine

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.