TabBar和NavigationBar的问题

时间:2019-03-17 06:28:11

标签: swift uinavigationcontroller uitabbarcontroller

我在TabBar中有两个视图控制器。我设置为如果用户已登录,则直接显示TabBar,否则显示loginViewController。在AppDelegate

中查看代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UserDefaults.standard.register(defaults: ["NSApplicationCrashOnExceptions": true])

        let status = UserDefaults.standard.bool(forKey: "status")

        //StoryBoard Decide
        if (status == false){
            let storyBoard : UIStoryboard = UIStoryboard(name: "Tools", bundle:nil)
            let nextViewController = storyBoard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
            let navigationController = UINavigationController(rootViewController: nextViewController)
            let appdelegate = UIApplication.shared.delegate as! AppDelegate
            appdelegate.window!.rootViewController = navigationController
        }else {
            let storyBoard : UIStoryboard = UIStoryboard(name: "Tools", bundle:nil)
            let nextViewController = storyBoard.instantiateViewController(withIdentifier: "tabbar") as! UITabBarController
            let navigationController = UINavigationController(rootViewController: nextViewController)
            let appdelegate = UIApplication.shared.delegate as! AppDelegate
            appdelegate.window!.rootViewController = navigationController
        }}

但是当我通过登录ViewController正常工作时,但是当用户登录时,它会在HomeViewController中显示导航栏。

enter image description here

这是我的故事板设置。 enter image description here

以及如何使用TabBar管理导航。

1 个答案:

答案 0 :(得分:2)

因为要制作新的导航控制器,然后将标签栏添加为其根视图。

您可以执行以下操作而不是制作UINavigationController:

替换

let nextViewController = storyBoard.instantiateViewController(withIdentifier: "tabbar") as! UITabBarController
let navigationController = UINavigationController(rootViewController: nextViewController)
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.window!.rootViewController = navigationController

具有:

let nextViewController = storyBoard.instantiateViewController(withIdentifier: "tabbar") as! UITabBarController
self.window?.rootViewController = nextViewController
self.window?.makeKeyAndVisible()