从代码中呈现tabBarController将删除导航栏按钮和标题

时间:2016-09-02 20:46:12

标签: ios swift xcode uinavigationcontroller uitabbarcontroller

我正在尝试在应用启动时使用代码(而不是storyboard segue)呈现tabBarController。如果用户已注册,则应在tabBarVC上打开应用程序,如果用户需要注册/登录,则会显示loginVC。我在didFinishLaunchingWithOptions

中称之为
if currentUser != nil {

    initialVC("tabBarVC")

} else {

    initialVC("loginVC")

}

并且initialVC函数是:

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()

}

当显示tabBarViewController时,导航栏按钮和标题不会显示。如果我直接转到tabBarController中的VC,导航栏会正确显示,但不会按预期显示标签栏。 如何展示tabBarVC并让navBars正确显示?

3 个答案:

答案 0 :(得分:1)

检查几点:

1)也许您对导航栏的设置是中性的(右侧顶部栏下拉列表)

enter image description here

2)如果步骤1不起作用,请尝试手动将navigationItem插入到viewController中:

enter image description here

答案 1 :(得分:1)

首先,如果您正在使用UITabBarController,则应直接初始化其属性viewControllers
其次,您不应将所有控制器向下转换为其基本类型。
第三,可能你可以使用这样的方案:Navigation(always root) - Initial Controller,如果没有登录,你可以推Login View controller

答案 2 :(得分:0)

我试图用tabBarController来展示navController。代码我最终使用工作

didFinishLaunchingWithOptions

if currentUser != nil {

    initialVC("tabBarVC")

} else {

    initialVC("loginVC")

}

功能:

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

    } 
}