我有一个在 UINavigationController 上运行的应用程序。现在我想在每个屏幕的底部添加一个 UIToolbar 元素。底部的工具栏应该可以针对当前显示的 ViewController 进行自定义。我的第一个想法是简单地将工具栏添加到 navigationController 视图并标记它,在 viewController 中我认为我可以检索 UIToolbar 元素。我有以下代码:
在我的 AppDelegate 中:
// Get instance of Toolbar (navController is an instance of UINavigationController and TOOLBAR_TAG a constant)
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 440, 320, 40)];
toolbar.tag = TOOLBAR_TAG;
[navController.view addSubview:toolbar];
在我的 viewController 我试过这个:
UIToolbar *toolbar = [self.navigationController.view viewWithTag:TOOLBAR_TAG];
toolbar.barStyle = UIBarStyleBlack;
然而,这给了我一个错误,说我的工具栏是“UILayoutContainerView”对象,而不是 UIToolbar 对象。因此,这个想法似乎是一个死胡同。
其他人是如何解决这个问题的?