[英]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.