1

I have looked really hard for this solution in Swift but am not coming up with one that works for me. I am trying to hide my "Admin" TabBarItem based on the permissions of the person that logs in to the app. I can disable it but it still shows up on the bar. I want to be able to show it for certain people and hide it for others. Also, when I print self.tabBarController?.viewControllers I get nil.

class TabBarMenuController: UITabBarController {


    let ref = Firebase(url: "")
    var position = ""

    func getPosition() {
        let userRef = ref.childByAppendingPath("users/\(ref.authData.uid)")
        userRef.observeSingleEventOfType(.Value, withBlock: {snapshot in
            if snapshot.value["position"] as! String != "Staff" {
                self.position = snapshot.value["position"] as! String

            }
        })

    }

    override func viewWillAppear(animated: Bool) {
        getPosition()
        print(self.tabBarController?.viewControllers)
        if position != "Staff" {
            if let tabBarController = self.tabBarController {
                let indexToRemove = 3
                if indexToRemove < tabBarController.viewControllers?.count {
                    var viewControllers = tabBarController.viewControllers
                    viewControllers?.removeAtIndex(indexToRemove)
                    tabBarController.setViewControllers(viewControllers, animated: true)
                }
            }

        }

    }

Also, I keep reading that this is against Apple's intended use. Is that true still? Is there a better workflow to accomplish that type of functionality?

Michael Williams
  • 1,402
  • 2
  • 14
  • 27

1 Answers1

0

I would create a tab that opens up the user's account and have a button in the user VC tab that opens up a page for admins only. you can show and hide the button as needed using adminButton.hidden = true or adminButton.hidden = false.

Dan Levy
  • 3,931
  • 4
  • 28
  • 48