presentViewController 后不显示导航栏

Navigation bar not showing after presentViewController

你好,我使用这段代码移动到新的控制器,只用代码,没有 segue 。

    UIStoryboard * mainstoryb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController * vc = [mainstoryb instantiateViewControllerWithIdentifier:@"online_shop"];
[self presentViewController:vc animated:YES completion:nil];

如何在下一个视图中显示导航栏或标题栏?

如果你想要导航栏,那么你必须推不存在

  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];


      SecondViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerB"];
    /********if dont have navigation bar***********/
 UINavigationController *initialNavigationController = (UINavigationController*)[self.storyboard instantiateViewControllerWithIdentifier:@"HomeNavCtrl_id"];
[self presentViewController:initialNavigationController animated:YES completion:nil];

/**********其他*************/

    [[self navigationController] pushViewController:vc animated:YES];

您需要按如下方式呈现 NavigationController :

 UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:ContactUSVCID];
 UINavigationController *objNav = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:objNav animated:YES completion:nil];

将您的 ViewController 嵌入到导航控制器中,并将故事板 ID 嵌入到导航控制器中,然后使用此代码

  UINavigationController *initialNavigationController = (UINavigationController*)[self.storyboard instantiateViewControllerWithIdentifier:@"HomeNavCtrl_id"];
[self presentViewController:initialNavigationController animated:YES completion:nil];

如果您展示一个视图控制器,它将始终全屏展示。 试试这个

UIStoryboard * mainstoryb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController * vc = [mainstoryb instantiateViewControllerWithIdentifier:@"online_shop"];
[self presentViewController:navigationController animated:YES completion:nil];