0

I'm using a Tab Bar Controller, it works fine, but when my app first starts with my Onboarding Screen for the users to accept the T&Cs, the tab bar disappears.

I'm not too sure what to do here. Here's my code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.


    //tnc screen ----------------------------------------------
    let launchedBefore = UserDefaults.standard.bool(forKey: "hasLaunched")
    self.window = UIWindow(frame: UIScreen.main.bounds)
    let launchStoryboard = UIStoryboard(name: "Onboarding", bundle: nil)
    let mainStoryboard = UIStoryboard (name: "Main", bundle: nil)

    var vc: UIViewController
    if launchedBefore {
        vc = mainStoryboard.instantiateInitialViewController()!


    } else {

        vc = launchStoryboard.instantiateViewController(withIdentifier: "FirstLaunch")

    }

    UserDefaults.standard.set(true, forKey: "hasLaunched")
    self.window?.rootViewController = vc
    self.window?.makeKeyAndVisible()
    //end tnc screen ---------------------------------------------

How should I fix this?

pckill
  • 3,709
  • 36
  • 48
Ali Wong
  • 1
  • 6
  • My guess is that your initialViewController from mainStoryboard is your UITabBarController - if you make another ViewController your rootViewController why would the TabBarController appear then? I think you should try to show your "FirstLaunch" ViewController from your TabBarController instead of configuring it as rootViewController – Teetz Feb 12 '19 at 09:05
  • @Teetz : ahh, okay, I think understand what you're saying. Correct me if I'm wrong, but it means that I should change this part of the code: `self.window?.rootViewController = vc`? – Ali Wong Feb 12 '19 at 09:27
  • Like Woodstock showed the ViewHierarchy. There are more ways to achieve what you need but i think the best is not to change the rootViewController (rootViewController is always TabBarController) and you just show your T&C ViewController as subView (presenting from TabBarController) – Teetz Feb 12 '19 at 09:37

2 Answers2

0

This is because you are obscuring your UITabBarController storyboard with the T&C storyboard.

I would be inclined to ditch story boards entirely, have a root UITabBarController, and on one of the UIViewControllers said TabBarController manages, add a sub view for T&C acceptance.

Ditching storyboards would look like this:

UIWindow
--UITabBarController (the windows root view controller)
----UIViewController for tab 1
------UIView to show T&C
----UIViewController for tab 2
----UIViewController for tab n
Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • Hmmm, does that mean that I should combine both into 1 storyboard? Sorry, I'm very new at this. – Ali Wong Feb 12 '19 at 09:24
  • @AliWong precisely. Or, get rid of storyboards (which are a visual way to do views) and instead build out your views programatically (in code). You don't need storyboards. You could just make a UITabBarController, set it as your Windows rootVC, and add more from there. I will update answer. – Woodstock Feb 12 '19 at 09:25
  • @AliWong let me know if this makes sense – Woodstock Feb 12 '19 at 09:27
  • Ohhhhh, I see, it does make a lot of sense. But if I were to do that, will I be able to see the other tabs when I first launch my app? Also, is there a way for me to do it with Storyboard? I don't think I'm familiar enough to do it programmatically. – Ali Wong Feb 12 '19 at 09:31
  • @AliWong yes you would see the other tabs when first launched if you did it this way. You can do it with storyboards, but you need it all in one! Hope this helps. – Woodstock Feb 12 '19 at 09:33
  • Oh dear, is there a way for me to do it without letting them see the other tabs? Thanks so much for your help so far! :) – Ali Wong Feb 12 '19 at 09:36
  • Every ViewController has an option `hidesBottomBarWhenPushed`. Use this to hide the TabBar. https://developer.apple.com/documentation/uikit/uiviewcontroller/1621863-hidesbottombarwhenpushed – Teetz Feb 12 '19 at 09:41
  • @Teetz : Ooo, I see I see. Okay, I'm understanding the whole logic behind this already, but I'm not sure how to move on with the logic. Is there a tutorial or article whereby I can follow to try it out? – Ali Wong Feb 13 '19 at 02:21
  • @AliWong I would recommend to just play around with some different view hierarchies and try it out. Just look at the different situations when changing rootViewController or presenting from TabBarController or even embedding a TabBarScene in NavigationController. – Teetz Feb 13 '19 at 06:32
0

I managed to fix it by adding this to my agree button (which is on the storyboard with my tncs):

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    self.present(homeVC, animated: true, completion: nil)*/
    let controller = storyboard.instantiateViewController(withIdentifier: "TabBarController"); addChild(controller)
    view.addSubview(controller.view)
    controller.didMove(toParent: self)

Huge thanks to @Woodstock and @Teetz for helping me understand the logic behind it! : )

Ali Wong
  • 1
  • 6