xcode 向导航栏添加另一个按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33764250/
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
Add another button to navigation bar
提问by m0j1
I'm modifying an app and this app has some buttons in its navigation bar, I wanted to add another button or Bar Button item by dragging it to the hierarchy but it seems I can't add it as a child to navigation bar, I wanted to ask if you have any solutions to this, here's a snapshot of the hierarchy of the storyboard, I want to add another Bar Button Item or another Button to the Navigation item.
Thanks
我正在修改一个应用程序,这个应用程序的导航栏中有一些按钮,我想通过将它拖到层次结构来添加另一个按钮或条形按钮项,但似乎我无法将它作为子项添加到导航栏中,我想请问您是否对此有任何解决方案,这是故事板层次结构的快照,我想在导航项中添加另一个 Bar Button Item 或另一个 Button。
谢谢
采纳答案by m0j1
Seems you can only have 3 elements on navigation bar with Interface Builder and you have to add more buttons via code. This link worked for me :
http://mandarapte.com/apple/add-navigation-bar-buttons-ios-app-xcode-project/
似乎您只能在带有 Interface Builder 的导航栏上有 3 个元素,并且您必须通过代码添加更多按钮。这个链接对我有用:http:
//mandarapte.com/apple/add-navigation-bar-buttons-ios-app-xcode-project/
回答by Gordonium
So I've just created a blank project and I can do this:
所以我刚刚创建了一个空白项目,我可以这样做:
I've also done stuff like this before where I actually use a UIButton rather than a bar button item gaining some extra customisability. Just make sure that you set an outlet to the button and in the view controller call self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.customButton];
:
在我实际使用 UIButton 而不是条形按钮项目之前,我也做过这样的事情,以获得一些额外的可定制性。只需确保为按钮和视图控制器调用设置了一个出口self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.customButton];
:
You can have multiple right bar button items and if they overlap the title (if you have one) then they are essentially clipped. From my understanding, you can have as many buttons as you like until the space is gone (which can vary depending on the device, obviously). Relevant docs:
您可以有多个右栏按钮项目,如果它们与标题重叠(如果有),那么它们基本上被剪裁了。根据我的理解,在空间消失之前,您可以拥有任意数量的按钮(显然,这可能因设备而异)。相关文档:
This array can contain 0 or more bar button items to display on the right side of the navigation bar. Items are displayed right-to-left in the same order as they appear in the array. Thus, the first item in the array is the rightmost item and other items are added to the left of the previous item.
If there is not enough room to display all of the items in the array, those that would overlap the title view (if present) or the buttons on the left side of the bar are not displayed.
此数组可以包含 0 个或多个栏按钮项以显示在导航栏的右侧。项目按照它们在数组中出现的相同顺序从右到左显示。因此,数组中的第一个项目是最右边的项目,其他项目添加到前一个项目的左侧。
如果没有足够的空间来显示数组中的所有项目,那些与标题视图(如果存在)或栏左侧的按钮重叠的项目将不会显示。
An example with lots of buttons:
一个有很多按钮的例子:
Programatically you can add buttons using:
您可以通过编程方式添加按钮:
navigationItem.rightBarButtonItems = [aButton, anotherButton, awesomeButton]
navigationItem.rightBarButtonItems = [aButton, anotherButton, awesomeButton]
Just make sure you test that the smallest device you target will still provide a good UX for the user if you add lots of buttons.
如果您添加大量按钮,请确保您测试目标最小的设备仍将为用户提供良好的用户体验。
回答by Mehdi S.
here is the correct answer for swift :
这是 swift 的正确答案:
let barButton_array: [UIBarButtonItem] = [Button1, Button2]
navigationItem.setRightBarButtonItems(barButton_array, animated: false)