我有一个带UIToolbar的UIViewController(在底部),我想在里面添加一个带UINavigationBar的UINavigationController。 但是不显示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];
}
答案 0 :(得分:16)
将视图控制器添加为子视图控制器是不够的。您还需要将导航控制器的视图添加为容器视图控制器视图的子视图。
[myNav willMoveToParentViewController:self];
myNav.view.frame = navFrame; //Set a frame or constraints
[self.view addSubview:myNav.view];
[self addChildViewController:myNav];
[myNav didMoveToParentViewController:self];
有关详细信息,请参阅the View Controller Programming Guide。
答案 1 :(得分:0)
对于 swift 5
let childNavController = UINavigationController()
parrentVC.addChild(childNavController)
parrentVC.view.addSubview(childNavController.view)
//Add constraints or frame for childNavController here.
childNavController.didMove(toParent: parrentVC)