简体   繁体   English

iOS-在UINavigationController上设置UIToolBarItem

[英]iOS - Set UIToolBarItem on UINavigationController

I drag out a toolbaritem in storyboard and set it on my nav controller, but when I run my code it's not there, is there something I'm missing? 我在情节提要中拖出一个工具栏项并将其设置在我的导航控制器上,但是当我运行代码时,它不存在,是否缺少某些内容?

EDIT: 编辑:

Tried setting it in code as well in my viewDidLoad method: 试图在我的viewDidLoad方法中的代码中进行设置:

UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Map" style:UIBarButtonItemStyleBordered target:self action:@selector(viewMap)];
self.navigationItem.rightBarButtonItem = rightBarButton;

Won't work either. 也不会工作。

Here's how it's set up in my storyboard: 这是在我的故事板中的设置方式:

在此处输入图片说明

UPDATE: 更新:

Just found my problem. 刚发现我的问题。 In my controller code when I update it's contents I change the right bar button item for a spinner and never set it back to what it had before. 在更新内容时,在控制器代码中,我更改了微调器的右侧栏按钮项,而从未将其设置回原来的状态。

UINavigationController already has a toolbar built in. It has a property toolBarHidden which is set to YES by default, which is why it is not normally seen. UINavigationController已经内置了一个工具栏。它具有一个属性toolBarHidden ,默认情况下将其设置为YES ,这就是通常不可见的原因。 If you are using storyboard you can easily make the built-in bottom toolbar visible by checking the checkbox "Shows Toolbar" in the inspector when the Navigation Controller is selected. 如果您正在使用情节提要,则可以通过选中选中“导航控制器”时在检查器中选中“显示工具栏”复选框来轻松使内置的底部工具栏可见。

See the UINavigationController documentation here for more details. 有关更多详细信息,请参见此处UINavigationController文档

EDIT: 编辑:

Ok, it sounds like what you are trying to do is add a right button to your view controller's UINavigationItem . 好的,听起来您要尝试的是在视图控制器的UINavigationItem添加一个右键。 To do this in storyboard, drag a "Bar Button Item" from the Objects Library onto the Navigation Item in your ViewController. 要在情节提要中执行此操作,请将“栏按钮项”从对象库拖到ViewController中的导航项上。 You can then set the title/style/etc of the bar button item. 然后,您可以设置条形按钮项目的标题/样式/等。 If nothing still shows up when you run your app, make sure that your ViewController is connected properly with a segue to the navigation controller. 如果在运行应用程序时仍未显示任何内容,请确保ViewController已正确连接,并且已正确连接到导航控制器。

Also make sure you are adding the Bar Button Item to your view controller's Navigation Item, NOT to the View Controller itself. 还要确保将“条形按钮”项添加到视图控制器的“导航项”,而不是添加到视图控制器本身。 Here is how the setup should look in your storyboard: 这是设置在故事板上的外观:

在此处输入图片说明

To add an item to a navigation bar, you need to add a Bar Button Item to the Navigation Item contained in the view controller. 要将项目添加到导航栏中,您需要将“ Bar Button Item添加到视图控制器中包含的“导航项目”中。 Go to your storyboard, find the right VC, and find the navigation item (it's in the hierarchy shown in the navigation controller 'scene'). 转到您的情节提要,找到合适的VC,然后找到导航项(它位于导航控制器“场景”中显示的层次结构中)。 Just drag a Bar Button Item into that hierarchy underneath the nav item, or directly onto the navbar in the visual builder display. 只需将Bar Button项拖到nav项下面的层次结构中,或直接拖到视觉生成器显示中的navbar上。

The navigation controller only looks at your VC's nav item when that VC is pushed onto the stack; 当该VC被推入堆栈时,导航控制器仅查看该VC的导航项。 hence modifying the VC's nav item in viewDidLoad has no effect. 因此,在viewDidLoad修改VC的导航项无效。

(I've done this programmatically before but I don't have the code with me, so maybe I'll add that later...) (我之前已经以编程方式完成了此操作,但是我没有代码,所以也许以后再添加...)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM