0

I have a tabBarController with three items to different viewControllers. There are four targets in my projects and for one of the targets I would like to add a new tabBar item that goes to newViewController. The item shouldn't show up when I run the other targets.

First I thought it was as easy as setting the newViewController to only be available to the specific target that I wanted, and that it would not show up in the tabBar if I ran the project under a different target. But the app crashed.

Is there a way to hide/show the tabBar item based on target without using the #if target code. We try to get away from that in the project. It would be nice to just do it in the storyBoard if that's possible. If not, then there is a custom tabBar class available. Let me know if you need to see some code from it.

  • I'm doing something similar to this in my project. But, I did everything programmatically. I don't know if that what you actually need. Based on condition `if else` I'm showing or hiding a `tabBarItem`. Is this what you need? – Frankenstein May 15 '20 at 11:49
  • We are staying away from if/else, but I actually managed to solve it by having two sets of storyboards, one with the extra item, and then assigning them to their respective target. – screenMonkey MonkeyMan May 15 '20 at 12:18
  • 1
    So in my case, I had different ids for different targets. I would compare the ids to check which target is currently executed and setup `tabBar` accordingly. I think my approach is much easier if you are able to do implement it this way. – Frankenstein May 15 '20 at 13:00
  • Ah, ok, so you set the id in the storyboards? But you still use conditionals to check target in the tabBar class? – screenMonkey MonkeyMan May 15 '20 at 15:08
  • Since this comment section was getting really long I have posted a small gist of my approach. – Frankenstein May 15 '20 at 15:43

1 Answers1

1

Since the comment section was getting really messy I thought I'd post the gist of how my approach for a different set of UITabBarItem for different target went. So firstly I created a static Environment variable for letting me know which target was getting executed. Here is the code:

enum Target {
    case targetOne, targetTwo

    static var current: Target {
        Bundle.main.bundleIdentifier?.contains("targetOneIdentifier") == true ? .targetOne : .targetTwo
    }
}

Then inside UITabBarController, I'm setting the viewControllers property according to the current target. This is some code in TabBarController:

class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let bool = Target.current == .targetOne
        let targetBasedViewController: UIViewController = bool ? FirstViewController() : SecondViewController()
        targetBasedViewController.tabBarItem.title = bool ? "First" : "Second"
        targetBasedViewController.tabBarItem.image = UIImage(named: bool ? "First" : "Second")
    }
}

Note: This is just the gist of customisation I did. The whole thing is really lengthy and would be really hard to understand considering the scenario.

Frankenstein
  • 15,732
  • 4
  • 22
  • 47