首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NavigationBar未出现

NavigationBar未出现
EN

Stack Overflow用户
提问于 2021-03-04 14:23:29
回答 1查看 40关注 0票数 0

我试着做一个有两个视图控制器的应用程序。第一个是LoginViewController,第二个是MainViewController,MainViewController有一个导航栏。我在SceneDelegate中设置了初始视图控制器,如下所示:

代码语言:javascript
复制
guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.windowScene = windowScene
        
 // a user is currently logged in

        if let _ = Auth.auth().currentUser {
          UIViewController.showViewController(storyBoardName: "Main", viewControllerId: "MainViewController")
        } else { 
            
 // no logged in user

          UIViewController.showViewController(storyBoardName: "Main", viewControllerId: "LoginViewController")
        }

        window?.makeKeyAndVisible()
      }
    }

这很好用,但是导航栏没有出现,我搜索了一下,发现初始的视图控制器应该是导航控制器,而不是MainViewController,所以我这样做:

代码语言:javascript
复制
guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.windowScene = windowScene
        
 // a user is currently logged in

        if let _ = Auth.auth().currentUser {
          UIViewController.showViewController(storyBoardName: "Main", viewControllerId: "MainViewController")
        } else { 
            
 // no logged in user

  let vc = MainViewController()
            let nav = UINavigationController(rootViewController: vc)
            nav.navigationBar.barTintColor = .white
            nav.navigationBar.isTranslucent = false
            nav.navigationBar.tintColor = .black
            self.window = UIWindow(frame: UIScreen.main.bounds)
            self.window!.rootViewController = nav
            self.window?.makeKeyAndVisible()
}
    }

但是,导航栏仍然没有显示

EN

回答 1

Stack Overflow用户

发布于 2021-03-04 22:15:59

在用户登录后,您必须以根用户身份创建MainViewController

代码语言:javascript
复制
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    
    guard let _ = (scene as? UIWindowScene) else { return }
    
    // a user is currently logged in
    
    if isUserLoggedIn {
        
        let vc = LoginViewController()
        let nav = UINavigationController(rootViewController: vc)
        nav.navigationBar.barTintColor = .white
        nav.navigationBar.isTranslucent = false
        nav.navigationBar.tintColor = .black
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window!.rootViewController = nav
        self.window?.makeKeyAndVisible()
        
    } else {
        let vc = ViewController()
        let nav = UINavigationController(rootViewController: vc)
        nav.navigationBar.barTintColor = .white
        nav.navigationBar.isTranslucent = false
        nav.navigationBar.tintColor = .black
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window!.rootViewController = nav
        self.window?.makeKeyAndVisible()
        
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66469656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档