当设置根视图控制器编程导航和标签栏丢失时

时间:2015-02-03 08:40:10

标签: ios iphone swift uinavigationcontroller uitabbarcontroller

App委托代码:

let storyboard = UIStoryboard(name: "Main", bundle: nil)

        let myViewController = storyboard.instantiateViewControllerWithIdentifier("MyViewController") as UIViewController
if self.window != nil {
                self.window!.rootViewController = myViewController

            }

当我在没有此代码的情况下加载应用程序时,一切都很好 但如果我运行此代码视图已加载但导航和标签栏丢失 我在这里想念的是什么? 我正在使用故事板。

2 个答案:

答案 0 :(得分:3)

这是可行的代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainRootController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as UIViewController
if self.window != nil {

                let navigationController:UINavigationController = storyboard.instantiateInitialViewController() as UINavigationController
                navigationController.viewControllers = [mainRootController]

                self.window!.rootViewController = navigationController
}
return true

答案 1 :(得分:0)

在添加根视图控制器之前,必须为导航控制器分配内存,然后导航控制器作为根视图控制器。

 UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:yourViewController];

             [[UINavigationBar appearance]setTintColor:[UIColor whiteColor]];

           self.window.rootViewController = navigationController;