xcode 如何在segueing后向可见的导航控制器添加按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25587465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add buttons to navigation controller visible after segueing?
提问by Jordan H
I have set up a view controller in Storyboard which is embedded in a navigation controller. In this nav controller I have checked Shows Navigation Bar and Shows Toolbar and enabled a navigation bar and toolbar in Top Bar and Bottom Bar respectively in Simulated Metrics. I then "Show e.g. Push" segue from the view controller to another view controller. On both view controllers I've set up Simulated Metrics the same way, so both the navigation bar and toolbar are visible on all three controllers.
我在 Storyboard 中设置了一个视图控制器,它嵌入在导航控制器中。在这个导航控制器中,我检查了显示导航栏和显示工具栏,并在模拟指标中分别在顶部栏和底部栏中启用了导航栏和工具栏。然后我从视图控制器到另一个视图控制器“显示例如推送”segue。在两个视图控制器上,我以相同的方式设置了模拟指标,因此导航栏和工具栏在所有三个控制器上都可见。
The problem is, I can add bar button items to the first view controller to both the navigation bar and the toolbar, but I cannot add a bar button item to the navigation bar on the second view controller. When I drop a button on the navigation bar, it's added to the toolbar instead. And I cannot double click the nav bar to add a title. In the outline I see there is no navigation item on the second view controller, but it is there in the first view controller.
问题是,我可以将栏按钮项添加到导航栏和工具栏的第一个视图控制器,但我无法向第二个视图控制器的导航栏添加栏按钮项。当我在导航栏上放置一个按钮时,它会被添加到工具栏中。而且我无法双击导航栏来添加标题。在大纲中,我看到第二个视图控制器上没有导航项,但它在第一个视图控制器中。
I can probably add buttons to the navigation bar programmatically, but I want to do this visually in Storyboard. My question is, what is wrong with this setup, or is this a bug with Xcode?
我可能可以以编程方式向导航栏添加按钮,但我想在 Storyboard 中直观地执行此操作。我的问题是,这个设置有什么问题,或者这是 Xcode 的一个错误?
回答by Ricky
For XCode 6, the UINavigationItem
for the 2nd view controller onwards is not added automatically on the View Controller Object inside the storyboard. You will have to drag the UINavigationItem
onto the Navigation Bar for that view Controller Object before adding UIBarButtonItem
on top of it.
对于 XCode 6,UINavigationItem
第二个视图控制器以后不会自动添加到情节提要内的视图控制器对象上。UINavigationItem
在添加之前,您必须将 拖到该视图控制器对象的导航栏UIBarButtonItem
上。
I am not sure why it is designed that way. I only discovered about this a few weeks ago.
我不确定为什么它是这样设计的。几周前我才发现了这一点。
回答by William T.
If you have a ton of view controllers and very little custom auto-layout stuff in place, you can disable size classes, then re-enable it will add all missing nav bars across your storyboard.
如果您有大量的视图控制器和很少的自定义自动布局内容,您可以禁用大小类,然后重新启用它将在您的故事板中添加所有丢失的导航栏。
Obviously not recommended if you have a ton of custom auto layout stuff linked up.
如果您链接了大量自定义自动布局内容,则显然不推荐。
If you have just a single UIViewController that's missing a nav bar, just drop a UINavigationItem onto it.
如果您只有一个缺少导航栏的 UIViewController,只需将 UINavigationItem 放到它上面。
回答by jrenouard
If you're using Xcode 7 beta 4/5, try restarting Xcode it solved the issue for me
如果您使用的是 Xcode 7 beta 4/5,请尝试重新启动 Xcode,它为我解决了问题
回答by ooxio
I just bumped into this issue and it seems to be affected by the segue that shows the view controller.
我刚刚遇到了这个问题,它似乎受到了显示视图控制器的 segue 的影响。
If "Kind" setting in segue is "Show (e.g. Push)", it's not possible to drag the item to top right corner. However, if you explicitly change "Kind" setting to "Push", this can be done.
如果 segue 中的“种类”设置为“显示(例如推送)”,则无法将项目拖动到右上角。但是,如果您明确将“种类”设置更改为“推送”,则可以这样做。
Edit: actually I just noticed that "Push" is deprecated. Not sure what Apple is thinking here. However, you can just change it back to "Show" after adding the button and it seems to work :D
编辑:实际上我只是注意到“推送”已被弃用。不知道苹果在这里想什么。但是,您可以在添加按钮后将其改回“显示”,它似乎有效:D
回答by Daniil Bystrov
For second view controller in hierarchy, you can setup title in attributes, without adding "navigation item"
对于层次结构中的第二个视图控制器,您可以在属性中设置标题,而无需添加“导航项”
回答by Hongyang
I think it is a bug. I had the same problem.I fixed this problem by disable the size classes, then enable it.
我认为这是一个错误。我有同样的问题。我通过禁用大小类来解决这个问题,然后启用它。
You can disable and enable the size classes in Interface builder doc.
您可以在 Interface builder doc 中禁用和启用尺寸类。
回答by emido333
It's very simple. You just need to use navigaitonItem first as the holder of the buttons and then you can add barButtonItems on top of that.
这很简单。您只需要首先使用 navigaitonItem 作为按钮的持有者,然后您就可以在其上添加 barButtonItems。