1

I am trying to implement bottom navigation bar for my iOS application. However, when I am creating tabBarItem, it is not showing on TabBar. TabBar is displaying correctly. I cannot figure out where is the problem, any help will be very appreciated.

If any additional information is required, please give me a sign. My code (simplified):

AppDelegate:

class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()

        window?.rootViewController = TabBarController()

        return true
    }
}

TabBarController:

class TabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let homeController = HomeController()
        let navigationController = UINavigationController(rootViewController: homeController)
        navigationController.title = "Home"
        navigationController.tabBarItem.image = UIImage(named: "icon")

        viewControllers = [homeController]
    } 
}

HomeController:

class HomeController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        self.tabBarController?.tabBar.isHidden = false
    }
}

EDIT:

I removed not crucial parts of the code, like isLoggedIn() function call, mentioned in the comments and changed MainNavigationController to TabBarController.

According to Matts answer I also changed this line in a TabBarController (but still bar item is not showing for some reason):

viewControllers = [navigationController]
Reindl
  • 23
  • 7
  • Nothing shows up even when isLoggedIn is true? It's easy to imagine isLoggedIn returning false and then nothing ever added to the tab bar, maybe move that block of code that sets the viewcontrollers outside the if? – Jacob Lange Apr 12 '18 at 17:39
  • @jake.lange I tried that, but isLoggedIn works properly. TabItem is not added to TabBar, even if code is placed before isLoggedIn. The problem must be somewhere else. – Reindl Apr 12 '18 at 17:43
  • Your mistake is a silly one (I am surprised that you didn't notice the missing navigation bar, which should have been a major clue), but the question itself was very well posed, as I was able to reproduce easily; you could in fact have eliminated even more and still supplied a reproducible case. – matt Apr 12 '18 at 17:56

1 Answers1

5

The problem is this line:

viewControllers = [homeController]

homeController is not navigationController. So what happened to navigationController? Nothing. It vanished in a puff of smoke. You created navigationController but then you threw it away.

So nothing you say about navigationController and its configuration (including its tab bar item) has any effect; it is not in the interface (or anywhere else).

This is my complete test code (based on your code):

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
        window?.rootViewController = MainNavigationController()
        return true
    }
}
class MainNavigationController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let homeController = HomeController()
        let navigationController = UINavigationController(rootViewController: homeController)
        navigationController.tabBarItem.title = "MyCoolTitle"
        viewControllers = [navigationController] // not [homeController]
    }
}
class HomeController: UIViewController {
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 3
    Also, it is a _really_ bad idea to name a UITabBarController MainNavigationController. It is _not_ a navigation controller, it's a tab bar controller. I suspect that this helped you to confuse yourself. – matt Apr 12 '18 at 17:53
  • Thanks for the answer and comments. I have not noticed that I totally did not use navigationController. But when I changed to viewControllers = [navigationController] tab item is still not showing for some reason. Maybe I am overlooking something else, I am quite new to iOS development. – Reindl Apr 12 '18 at 18:17
  • Well, at that point I can't help. With that correction, in my version of your code, the tab item did appear. Perhaps you have no image called `"icon"`? My test involved giving the tab bar item a `title`, to eliminate that possibility. – matt Apr 12 '18 at 18:34
  • Added my complete test code, proving that my fix works — based on what you've said. If there's more you have not told us, I can hardly be expected to be telepathic about that. – matt Apr 12 '18 at 18:45
  • Yes it is indeed working great. So the second problem with my code must be somewhere else. I will try to catch it, very thanks for your help! – Reindl Apr 12 '18 at 18:49