0

I'm beginner. I'm coding an iPhone app in swift 4 using storyboard. I've tab bar which shows different items for logged out users and logged in users. I want to know what is the best practice to show these different tab bar items for logged in and logged out users.

I can think of following solution: Have one set of tab bar items and show the ones which are required for logged in and logged out users programmatically.

Can anyone suggest best practice to handle this situation in storyboard and/or programatically?

Tx

Mike
  • 41
  • 2
  • 1
    You could search better, see this [link](https://stackoverflow.com/questions/35912168/how-to-dynamically-populate-tab-bar-controllers-tab-bars-in-swift-ios-9) – Arash Etemad Apr 16 '18 at 09:29
  • https://stackoverflow.com/questions/5909727/how-to-remove-programmatically-a-tab-bar-item-created-in-parent-class-nib-file – Nitish Apr 16 '18 at 09:31

2 Answers2

2

You can subclass UITabBarController and change viewControllers property

class customTabViewController: UITabBarController {


    override func awakeFromNib() {

        if(logged)
        {
            self.viewControllers = [v1,v2,v3]
        }
        else
        {
            self.viewControllers = [v1,v2]
        }
    }

}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Tx, that helps. What about storyboard? What we do in storyboard? Shall we just show some tab items in storyboard and control rest from the code? – Mike Apr 16 '18 at 10:45
  • whether you show tabs or not in storyboard the line in awakeFromNib will override it – Shehata Gamal Apr 16 '18 at 10:58
0

Another approach: Say you have tabs A,B,C,D. All these are available(enabled) when a user logs in, while only A,B,C are available (enabled) when the user has logged out.

Prashant
  • 336
  • 3
  • 18
  • Sorry, did you miss the actual solution or I didn't get it? Are you saying that create all tabs in tabbar storyboard and show the required tabs when user is logged in or logged out? That can be one idea but how do you do it in the code? – Mike Apr 16 '18 at 10:44
  • please have a look at the following link. Hope this helps: https://stackoverflow.com/questions/10233715/disable-enable-tabs-in-uitabbarcontroller-in-storyboard-based-app?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Prashant Apr 16 '18 at 11:02
  • Tx Prashant, I'll check it out. – Mike Apr 16 '18 at 14:01