Reputation: 4281
I am trying to present a tabBarController using code (not storyboard segue) when the app starts up. If a user is registered, the app should open on the tabBarVC and if a user needs to signup/login, the loginVC is presented. I call this in didFinishLaunchingWithOptions
:
if currentUser != nil {
initialVC("tabBarVC")
} else {
initialVC("loginVC")
}
and the initialVC function is:
func initialVC(storyboardID: String) {
let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("\(storyboardID)") as UIViewController
let navController = UINavigationController(rootViewController: initialViewController)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
}
When the tabBarViewController is presented, the nav bar buttons and titles don't show up. If I segue directly to a VC within the tabBarController, the nav bar shows correctly, but the tab bar isn't presented, as expected. How can I present the tabBarVC and get the navBars to show up correctly?
Upvotes: 1
Views: 38
Reputation: 4281
I was trying to present the tabBarController
with a navController
. The code I ended up using that works.
In didFinishLaunchingWithOptions
:
if currentUser != nil {
initialVC("tabBarVC")
} else {
initialVC("loginVC")
}
The function:
func initialVC(storyboardID: String) {
let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("\(storyboardID)") as UIViewController
self.window?.makeKeyAndVisible()
if storyboardID == "tabBarVC" {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewController
} else {
let navController = UINavigationController(rootViewController: initialViewController)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = navController
}
}
Upvotes: 0
Reputation: 12910
Check few points:
1) Maybe your settings about navbar are neutral (Top Bar dropdown on the right)
2) If step no 1 does not work, try to insert navigationItem manually into your viewController:
Upvotes: 1
Reputation: 330
First, if you are using UITabBarController, you should initialise its property viewControllers
directly.
Second, you should not downcast all controller to its base type.
Third, possibly you may use such scheme: Navigation(always root)
- Initial Controller
, and if there is no login you push Login View controller
Upvotes: 1