ios - 在 subview Controller 中显示导航栏

标签 ios uinavigationcontroller uinavigationbar childviewcontroller splitview

我正在开发一个应用程序,我必须在其中并排显示多个 View Controller ( Split View)。为此,我添加了 View 作为 subview Controller 。

目标:我想在一个 subview Controller 上显示导航栏,同时在父 View Controller 上显示单独的导航栏。

问题: 导航栏未显示在 subview Controller 上。

编辑:我还将父 View Controller 的导航栏设置为隐藏,但是当调用 subview Controller 时,导航栏出现在父 View Controller 上,而不是 subview Controller 上。

添加 subview Controller 的代码是:

    MyChildViewController *childViewController = [[MyChildViewController alloc] initWithNibName:@"MyChildViewController" bundle:nil];

    [self addChildViewController:childViewController];
    [childViewController.view setFrame:CGRectMake(0.0f, 0.0f, self.rightContainerView.frame.size.width, self.rightContainerView.frame.size.height)];
    [self.rightContainerView addSubview:childViewController.view];
    [childViewController didMoveToParentViewController:self];

此代码运行良好, subview Controller 可以完美添加。我想知道是否可能?

提前致谢。

最佳答案

我通过以下方式自行解决了这个问题:

MyChildViewController *childViewController = [[MyChildViewController alloc] initWithNibName:@"MyChildViewController" bundle:nil];
[childViewController.view setFrame:CGRectMake(0.0f, 0.0f, self.rightContainerView.frame.size.width, self.rightContainerView.frame.size.height)];

UINavigationController *childNavController = [[UINavigationController alloc] initWithRootViewController:childViewController];
childNavController.view.frame = childViewController.view.frame;

[self addChildViewController:childNavController];
[self.rightContainerView addSubview:childNavController.view];
[childNavController didMoveToParentViewController:self];

现在,当我在 MyChildViewController 中添加导航栏时,它会添加到 subview Controller 中,并且不会影响父 View Controller 的导航栏。 subview Controller 的 navigationController 属性也不同于父 View Controller 的 navigationController 属性,两者都有自己的导航堆栈。

关于ios - 在 subview Controller 中显示导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22935229/

相关文章:

iphone - 如何使用 NSUserDefaults 保存两个文本字段的数据?

ios - UIView 是 UIScrollView 的 subview 。如何跟踪 subview 在定义的矩形内?

iOS 重置重用的 ViewController 或 ASIHTTPRequest 问题?

ios - 返回上一个 VC 后导航栏会发生变化

iOS - UINavigationController - NavigationBar 与 UIViewController 一起滑入

ios - 在相机 OverlayView 中绘制图像

objective-c - iOS 似乎绕过了基本的服务器身份验证

ios - 后退按钮没有相应的颜色( objective-c )

html - 如何在当前菜单中制作下拉菜单

iOS 11.2自定义导航栏死循环