问题描述
我有一个在 UINavigationController 上运行的应用程序.现在我想在每个屏幕的底部添加一个 UIToolbar 元素.底部的工具栏应该可以针对当前显示的 ViewController 进行自定义.我的第一个想法是简单地将工具栏添加到 navigationController 视图并标记它,在 viewController 中我认为我可以检索 UIToolbar 元素.我有以下代码:
I have an application which runs on a UINavigationController. Now I would like to add a UIToolbar element to the bottom of each screen. The Toolbar on the bottom should the be customizable for the ViewController that is currently being displayed. My first idea was to simply add the toolbar to the navigationController view and tag it, in the viewController I thought I would then be able to retrieve the UIToolbar element. I have the following code:
在我的 AppDelegate 中:
In my 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 我试过这个:
In my viewController I tried this:
UIToolbar *toolbar = [self.navigationController.view viewWithTag:TOOLBAR_TAG];
toolbar.barStyle = UIBarStyleBlack;
但这给了我一个错误,说我的工具栏是UILayoutContainerView"对象,而不是 UIToolbar 对象.因此,这个想法似乎是一个死胡同.
Yet this gives me an error saying that toolbar in my case is a "UILayoutContainerView" object, not an UIToolbar object. Hence this idea seems to be a dead end.
其他人是如何解决这个问题的?
How did others solve this issue?
推荐答案
UINavigationController 已经有了工具栏.只需使用
UINavigationController already has a toolbar. Just use
[self.navigationController setToolbarHidden:NO];
在最顶层的视图控制器和
in the topmost view controller and
[self setToolbarItems:items];
在所有视图控制器中,其中 items 是该视图控制器工具栏项的 NSArray
.
in all your view controllers, where items is an NSArray
of that view controller's toolbar items.
至于为什么您的解决方案不起作用:您的 TOOLBAR_TAG
可能不是唯一的,这就是您获得另一个子视图的原因.但正如我所说,无论如何你都应该使用附带的工具栏.
As for why your solution isn't working: your TOOLBAR_TAG
is probably not unique, that's why you're getting another subview. But as I said, you should use the included toolbar anyway.
这篇关于UINavigationController每一页的UIToolbar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!