cesarcarlos cesarcarlos - 1 year ago 43
Swift Question

Making a conditional tab bar in ios

I'm working on an IOS app that uses tabs for navigation. The app gives access to users to a video library. However there are two types of users, those who purchase individual episodes and those who are subscribed. The former only have access to the videos they purchased while the latter have access to every single video in the library.

In my tab bar (in storyboard) I have a Purchases button, but if the user is a subscriber I don't want this tab to appear.

The app checks if a user is logged in upon launching and checks to see what the user status is (buyer or subscriber). I would like to know if there is a way to load different sets of tabs depending on the user type.

If any one could steer me in the right direction I'd really appreciate it. Thanks!

Answer Source

Subclass UITabBarController and use setViewControllers(_:animated:):

class MyTabBarController: UITabBarController 
{

    override func viewDidLoad() 
    {
        super.viewDidLoad()

        switch user
        {
        case .buyer:
            guard let vc1 = storyboard?.instantiateViewController(withIdentifier: "first"),
                  let vc2 = storyboard?.instantiateViewController(withIdentifier: "second") else
            {
                return
            }

            setViewControllers([vc1, vc2], animated: true)

        case .subscriber:
            guard let vc3 = storyboard?.instantiateViewController(withIdentifier: "third"),
                  let vc4 = storyboard?.instantiateViewController(withIdentifier: "fourth") else
            {
                return
            }

            setViewControllers([vc3, vc4], animated: true)
        }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download