Ios 显示视图时导航栏不显示?

Ios 显示视图时导航栏不显示?,ios,swift,storyboard,Ios,Swift,Storyboard,在另一个文件中,我从另一个故事板创建了一个视图控制器实例,并将其显示为: let viewController:UIViewController = UIStoryboard(name: "UserProfile", bundle: nil).instantiateViewControllerWithIdentifier("profileID") as! ProfileViewController self.presentViewController(viewController, animat

在另一个文件中,我从另一个故事板创建了一个视图控制器实例,并将其显示为:

let viewController:UIViewController = UIStoryboard(name: "UserProfile", bundle: nil).instantiateViewControllerWithIdentifier("profileID") as! ProfileViewController
self.presentViewController(viewController, animated: true, completion: nil)
在我的故事板中,我有正确的故事板ID,并将我的视图控制器嵌入到UINavigationController中。我还将初始视图控制器设置为UINavigationController


为什么它没有显示?

创建视图控制器的对象,然后将导航控制器添加到其中,然后显示它:

let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("MyViewController") as! ViewController
let navController = UINavigationController(rootViewController: VC1) // Creating a navigation controller with VC1 at the root of the navigation stack.
self.presentViewController(navController, animated:true, completion: nil)

好的,您现在展示的是视图控制器(请注意,它不是导航控制器)

您可以做什么:

  • 按下视图控制器

    let viewController:UIViewController = UIStoryboard(name: "UserProfile", bundle: nil).instantiateViewControllerWithIdentifier("profileID") as! ProfileViewController
    [self.navigationController pushViewController:viewController animated:YES];
    
  • 演示导航控制器

    let viewController:UIViewController = UIStoryboard(name: "UserProfile", bundle: nil).instantiateViewControllerWithIdentifier("profileID") as! ProfileViewController
    
    let navigationController = UINavigationController(rootViewController: viewController)
    
    self.presentViewController(viewController, animated: true, completion: nil)
    

  • 对于仍然使用Objective-C的用户:

    UIStoryboard *sb = [UIStoryboard storyboardWithName: @"Storyboard" bundle: [NSBundle mainBundle]];
    ViewController *vc = [sb instantiateInitialViewController];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController: vc];
    [self presentViewController:nc animated:YES completion:nil];
    
    别忘了按下按钮:


    您也可以使用objective-c执行相同的操作。