9

我有一个带有 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];
}
4

2 回答 2

17

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

[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 编程指南

于 2013-07-22T03:12:23.890 回答
1

对于迅捷5

let childNavController = UINavigationController()
parrentVC.addChild(childNavController)
parrentVC.view.addSubview(childNavController.view)
//Add constraints or frame for childNavController here.
childNavController.didMove(toParent: parrentVC)
    
于 2021-01-25T14:12:22.337 回答