UINavigationController各页面上的UIToolbar

发布于 2024-08-22 16:12:35 字数 818 浏览 9 评论 0原文

我有一个在 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 对象。因此,这个想法似乎是一个死胡同。

其他人是如何解决这个问题的?

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:

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];

In my viewController I tried this:

UIToolbar *toolbar = [self.navigationController.view viewWithTag:TOOLBAR_TAG];
toolbar.barStyle = UIBarStyleBlack;

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

短暂陪伴 2024-08-29 16:12:35

UINavigationController 已经有一个工具栏。 所有视图控制器中使用

[self.navigationController setToolbarHidden:NO];

只需在最顶层视图控制器和

[self setToolbarItems:items];

,其中 items 是该视图控制器工具栏项的 NSArray 。

编辑:至于您的解决方案不起作用的原因:您的TOOLBAR_​​TAG可能不是唯一的,这就是您获得另一个子视图的原因。但正如我所说,无论如何您都应该使用附带的工具栏。

UINavigationController already has a toolbar. Just use

[self.navigationController setToolbarHidden:NO];

in the topmost view controller and

[self setToolbarItems:items];

in all your view controllers, where items is an NSArray of that view controller's toolbar items.

EDIT: 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.

柏林苍穹下 2024-08-29 16:12:35

要轻松显示 UINavigationController 底部工具栏,您可以单击“显示工具栏”复选框,在选中“导航控制器”对象的情况下,可以从检查器访问该复选框。我希望这会有所帮助:)

To easily display the UINavigationController bottom toolbar, you can click on the "Show Toolbar" checkbox which is reachable from the inspector with "Navigation Controller" object selected. I hope this may help :)

残疾 2024-08-29 16:12:35

从设计角度来看,我不会推荐这样做,除非您知道堆栈中的每个视图都会显示工具栏。当您开始隐藏/显示堆栈中不同视图的工具栏时,您会发现该动画(工具栏随视图滑出/滑入)不会像您期望的那样进行动画处理。

如果您需要特定视图的工具栏,请将它们放入这些视图中,因为工具栏与视图相关,而不是与整个导航堆栈相关。

From a design perspective I would not recommend this UNLESS you know the toolbar will be present for each view in the stack. The second you start hiding/showing the toolbar for different views in the stack you will see that that animation (toolbar slides out/in with views) doesn't animate like you expect.

If you need toolbars for specific views put them in those views, since toolbar are contextual to the view, not to the nav stack as a whole.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文