简体   繁体   English

将工具栏添加到导航ViewController的正确方法是什么?

[英]What is the correct way to add a toolbar to a navigation viewcontroller?

So I'm trying to add a UIToolbar to a UIViewController that is part of a UINavigation hierarchy and I was wondering what is the best way to do this. 所以我试图将UIToolbar添加到UINavigation层次结构的一部分UIViewController中,我想知道什么是最好的方法。 I know in iOS3 they enabled each viewcontroller that is part of a navigation hierarchy to have it's own toolbar so I figure this is the best way to do it. 我知道在iOS3中,他们使作为导航层次结构一部分的每个viewcontroller拥有自己的工具栏,因此我认为这是最好的方法。 However, the syntax of how I'm doing is bugging me, as I'm using three different types of syntax to add the toolbar as follows: 但是,我正在使用的语法令人烦恼,因为我使用三种不同类型的语法来添加工具栏,如下所示:

[[self navigationController] setToolbarHidden:NO]; 
[self setToolbarItems: myToolbarButtons];
[[[self navigationController] toolbar]setBarStyle:UIBarStyleBlack];

This works fine, and actually fixes a bad memory access from when I was adding the toolbar to the subview of the navigation view. 从我将工具栏添加到导航视图的子视图起,这可以正常工作,并且实际上修复了错误的内存访问。 But I don't understand how I can do "self setToolbarItems" after I make the toolbar visible. 但是我不知道如何使工具栏可见后如何执行“ self setToolbarItems”。 Does it become a part of the viewcontroller then? 它将成为视图控制器的一部分吗? Like I said, this works but is bugging me. 就像我说的那样,这行得通,但是困扰着我。

An UIVIewController has a property called UINavigationController. UIVIewController具有一个称为UINavigationController的属性。 Each UINavigationController has a built-in toolbar of their own. 每个UINavigationController都有自己的内置工具栏。 So when you are calling, 所以当你打电话时

[[self navigationController] setToolbarHidden:NO];

you are actually enabling the toolbar of the navigationController property that comes with each UIViewController. 您实际上是在启用每个UIViewController随附的navigationController属性的工具栏。 And when you set the toolbar's item and style with the following two lines: 当您使用以下两行设置工具栏的项目和样式时:

[self setToolbarItems: myToolbarButtons];
[[[self navigationController] toolbar]setBarStyle:UIBarStyleBlack];

you are actually setting the items of that UINavigationController's built-in toolbar. 您实际上是在设置该UINavigationController的内置工具栏的项目。

Hope this helps. 希望这可以帮助。 Check out the UIViewController Class Reference for more information. 请查看《 UIViewController类参考》以获取更多信息。

In line 1 you're sending a message to the toolbar's superview to hide it which is done this way because then the superview can do whatever resizing/layout it wants at the best time. 在第1行中,您正在向工具栏的超级视图发送一条消息以隐藏它,这是通过这种方式完成的,因为这样,超级视图就可以在最佳时间进行所需的任何调整大小/布局。

The navigation controller reads the toolbar from self 's variables in line 2, because the navigation controller is currently presenting self . 导航控制器从第2行中self的变量读取工具栏,因为导航控制器当前正在显示self

In line 3 you're accessing the toolbar itself, managed by navigationController . 在第3行中,您将访问由navigationController管理的工具栏本身。 This is a part of the toolbar that relates to itself and not the navigation controller, so the navigation controller doesn't expose ways to modify it, allowing you to access the toolbar directly and then modify it. 这是工具栏的一部分,与自身相关,与导航控制器无关,因此导航控制器没有公开修改工具的方式,允许您直接访问工具栏然后进行修改。

This way you are accessing the toolbar at various levels making it easier on yourself. 这样,您可以在各个级别访问工具栏,从而使自己更轻松。 For In the first line you don't have to manage displaying it. 对于在第一行中,您不必管理显示它。 In the second and third lines you don't have to manage creating/destroying it. 在第二和第三行中,您不必管理创建/销毁它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将ToolBar添加到UITableView的正确方法是什么? - What's the right way to add a ToolBar to a UITableView? 将按钮添加到导航工具栏 - Add a button to a navigation toolbar iOS中的导航工具栏有什么意义? - What is the point of a navigation toolbar in iOS? 如何使用导航 controller 以这种方式渲染视图控制器 - How to render viewcontroller in this way using navigation controller 将导航控制器添加到viewcontroller,它是窗口的rootviewcontroller - add navigation controller to viewcontroller which is the rootviewcontroller of the window iPhone - 为不同类型的视图/窗口设置应用程序导航的正确方法是什么? - iPhone - What is the correct way to setup app navigation for different types of views/windows? 从导航控制器堆栈中删除所有Viewcontroller的最佳方法 - Best way to remove all the Viewcontroller from navigation controller stack 如何在viewcontroller中添加导航控制器和标签栏控制器? - how to add navigation controller and tab bar controller within viewcontroller? 正确的传播方式应该在iOS6中自动旋转成深度模态ViewController - Correct way to propagate shouldAutorotate into a deep modal viewcontroller in iOS6 与ABPerson联系的正确方法是什么? - What is the correct way to associate with a ABPerson?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM