本文介绍了UINavigationController的每个页面上的UIToolbar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在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];
[self setToolbarItems: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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 20:11