Code Sherpa Code Sherpa -4 years ago 247
Swift Question

Getting Black Screen with navigationController?.pushViewController (Swift 3.0)

I am having problems programmatically sending a user from one view controller to another. I am posting the code associated with he flow below. In addition to letting me know what the correct code is (which I would appreciate) I'd also be interested if the logic/design itself seems OK.

I am controlling my UI programmatically. Accordingly, in my app delegate didfinnishlaunchingwithoptions I have

window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = SigninController()

When the user opens the app, they are redirected to the SigninController.
Then, inside of SigninController, I am handling all of the social authentication stuff against Firebase. I have a listener in my code to confirm that the user is (or is not) authenticated and sends him along:

let provider: [FUIAuthProvider] = [FUIGoogleAuth(), FUIFacebookAuth()]
FUIAuth.defaultAuthUI()?.providers = provider

// listen for changes in the authorization state
_authHandle = FIRAuth.auth()?.addStateDidChangeListener { (auth: FIRAuth, user: FIRUser?) in
// check if there is a current user
if let activeUser = user {

// check if the current app user is the current FIRUser
if self.user != activeUser {
self.user = activeUser
self.signedInStatus(isSignedIn: true)
print("user session is active, redirecting...")
let nextViewController = CustomTabBarController()
self.navigationController?.pushViewController(nextViewController, animated: true)

} else {
// user must sign in
self.signedInStatus(isSignedIn: false)



In the above code, if the user is confirmed as signed in, then I use the below code to send them along. This is where I am having the problem. Right now I just see a black screen but no error message.

let nextViewController = CustomTabBarController()
self.navigationController?.pushViewController(nextViewController, animated: true)

And here is the code for the CUstomTabBarController class.

class CustomTabBarController : UITabBarController
override func viewDidLoad()

let home = createNavController(imageName: "gen-home", rootViewController: HomeController(collectionViewLayout: UICollectionViewFlowLayout()))
let loc = createNavController(imageName: "loc-map-route", rootViewController: LocController(collectionViewLayout: UICollectionViewFlowLayout()))
let stats = createNavController(imageName: "pre-bar-chart", rootViewController: StatsController(collectionViewLayout: UICollectionViewFlowLayout()))
let profile = createNavController(imageName: "account", rootViewController: ProfileController(collectionViewLayout: UICollectionViewFlowLayout()))

viewControllers = [home, loc, stats, profile]


private func createNavController(imageName: String, rootViewController: UIViewController) -> UINavigationController
let navController = UINavigationController(rootViewController: rootViewController)
navController.tabBarItem.image = UIImage(named: imageName)

return navController


I am sure I am overlooking something silly, but sometimes it takes another pair of eyes to point it out.

Thanks in advance.

Answer Source


self.navigationController?.pushViewController(nextViewController, animated: true)

there is no navigationController.

In your AppDelegate you need to do this:

let rootViewController = SigninController()
let navController: UINavigationController = UINavigationController(rootViewController: rootViewController)
self.window?.rootViewController = navController

It's better to assign rootViewController to the window before making it visible. Otherwise there may be screen blinks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download