Home > Back-end >  Navigation Controller being Black Screen in swift 5
Navigation Controller being Black Screen in swift 5

Time:03-17

I'm not using storyboard. I'm embedding my MainViewController into a navigation controller.

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    let mainVC = MainViewController(nibName: nil, bundle: nil)
    let navigationController = UINavigationController(rootViewController: mainVC)
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()
}

But it's only showing black screen instead of my mainVC.

CodePudding user response:

You should use window like this

guard let windowScene = (scene as? UIWindowScene) else { return }
    let window = UIWindow(windowScene: windowScene)

CodePudding user response:

This below code finally works for me.

In SceneDelegate,

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    let rootViewController = MainViewController()
    let navController: UINavigationController = UINavigationController(rootViewController: rootViewController)
    self.window?.rootViewController = navController
    self.window?.makeKeyAndVisible()
}

In MainViewController, this code is taken from @Kudos from comment.

override func viewDidLoad() {
    super.viewDidLoad()
    if #available(iOS 15, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        self.navigationController?.navigationBar.isTranslucent = true  // pass "true" for fixing iOS 15.0 black bg issue
        self.navigationController?.navigationBar.tintColor = UIColor.white // We need to set tintcolor for iOS 15.0
        appearance.shadowColor = .clear    //removing navigationbar 1 px bottom border.
        UINavigationBar.appearance().standardAppearance = appearance
        UINavigationBar.appearance().scrollEdgeAppearance = appearance
    }
}
  • Related