在UIViewController中添加UINavigationController

我有一个UIToolbar与UIToolbar(底部),我想添加一个UINavigationController与UINavigationBar里面。 但是UINavigationController不显示。

MyViewController.m:

- (void)viewDidLoad { [super viewDidLoad]; int toolBarHeight = 44; UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, [self.view bounds].size.height-toolBarHeight, [self.view bounds].size.width, toolBarHeight)]; UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:nil action:nil]; toolBar.items = @[button]; [self.view addSubview:toolBar]; MyNavigationController *myNav = [[MyNavigationController alloc] init]; [self addChildViewController:myNav]; } 

添加视图控制器作为子视图控制器是不够的。 您还需要将导航控制器的视图添加为容器视图控制器视图的子视图。

 [myNav willMoveToParentViewController:self]; myNav.view.frame = navFrame; //Set a frame or constraints [self.view addSubview:myNav.view]; [self addChildViewController:myNav]; [myNav didMoveToParentViewController:self]; 

有关更多详细信息,请参阅“View Controller编程指南”