iOS - UINavigationController 添加多个正确的项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6885487/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:54:44  来源:igfitidea点击:

iOS - UINavigationController adding multiple right items?

iphoneobjective-ciosuinavigationcontroller

提问by aryaxt

I have a UINavigationController. I'm trying to add multiple buttons on the right side of my navigationBar. How can I achieve this? What kind of button does it take? UIBarButtonor UINavigationItem?

我有一个UINavigationController. 我正在尝试在导航栏的右侧添加多个按钮。我怎样才能做到这一点?它需要什么样的按钮?UIBarButton或者UINavigationItem

采纳答案by Dean Davids

I am sure I read in the developer reference that additional buttons in the navigation bar is frowned upon. I cannot find that passage now. I have not done it myself, but found this link that seems to outline exactly what you need to do: (http://www.mattdipasquale.com/blog/2010/11/02/how-to-add-multiple-uibarbuttonitems-to-uinavigationbar/)

我确信我在开发人员参考中读到导航栏中的其他按钮不受欢迎。我现在找不到那个段落了。我自己没有做过,但发现这个链接似乎准确地概述了您需要做的事情: (http://www.mattdipasquale.com/blog/2010/11/02/how-to-add-multiple-uibarbuttonitems -to-uinavigationbar/)

Have you considered using the toolbar property of the navigation controller?

您是否考虑过使用导航控制器的工具栏属性?

回答by jrturton

As of iOS5 you can assign an array of bar button items to the navigation item's rightBarButtonItems(note the plural) property.

从 iOS5 开始,您可以将一组条形按钮项分配给导航项的rightBarButtonItems(注意复数)属性。

回答by Denis Kutlubaev

I used JRTurtons answer in Xcode 4.5, iOS 6 and implemented it like this and it works:

我在 Xcode 4.5、iOS 6 中使用了 JRTurtons 答案并像这样实现它并且它有效:

// Two buttons at the right side of nav bar
UIBarButtonItem *addAttachButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAttachmentClicked:)];
UIBarButtonItem *sendButton = [[UIBarButtonItem alloc] initWithTitle:LS(@"Send") style:UIBarButtonItemStyleBordered target:self action:@selector(sendClicked:)];
self.navigationItem.rightBarButtonItems = @[addAttachButton,sendButton];

However, I should mention, that UIBarButtonSystemItemPageCurl doesn't work like that.

但是,我应该提到, UIBarButtonSystemItemPageCurl 不是这样工作的。

回答by mihai

adding any design to the navigation controller in XCode is easy.

在 XCode 中向导航控制器添加任何设计都很容易。

add a UIView to your scene add the buttons you need to the UIView then drag and drop the UIView to the right space in the navigationController

将 UIView 添加到您的场景中 将您需要的按钮添加到 UIView 然后将 UIView 拖放到 navigationController 中的正确空间

回答by Rick

In Xcode 7.1 (perhaps even earlier), you can add multiple items to the right or left side of a UINavigationBar just by dragging them in. If you drag to just the right spot, you get a little vertical bar that indicates where that item will be inserted.

在 Xcode 7.1(也许更早)中,您可以通过将多个项目拖入 UINavigationBar 的右侧或左侧来添加它们。如果您拖动到正确的位置,您会得到一个小垂直条,指示该项目将在哪里被插入。

回答by Anton

there's actually even a bit ore hacky, but at the same time more cleaner way of doing this stuff: just define a category on UINavigationItem, like:

实际上甚至还有一点点 hacky,但同时做这些事情的方法更简洁:只需在 UINavigationItem 上定义一个类别,例如:

UINavigationItem+Toolbars.h:

UINavigationItem+Toolbars.h:

@interface UINavigationItem (Toolbars)

@property (nonatomic, retain) IBOutlet UIToolbar * rightToolBar;
@property (nonatomic, retain) IBOutlet UIToolbar * leftToolBar;

- (void)setRightToolBar:(UIToolbar *)_rightToolBar;
- (UIToolbar *)rightToolBar;
- (void)setLeftToolBar:(UIToolbar *)_leftToolBar;
- (UIToolbar *)leftToolBar;

@end

UINavigationItem+Toolbars.m:

UINavigationItem+Toolbars.m:

#import "UINavigationItem+Toolbars.h"

@implementation UINavigationItem (Toolbars)

- (void)setRightToolBar:(UIToolbar *)_rightToolBar {
    self.rightBarButtonItems = _rightToolBar.items;
}

- (UIToolbar *)rightToolBar {
    return nil;
}

- (void)setLeftToolBar:(UIToolbar *)_leftToolBar {
    self.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:_leftToolBar] autorelease];
}

- (UIToolbar *)leftToolBar {
    return nil;
}

@end

After doing that, just assign an outlet in IB settings a toolbar (just create one) and enjoy buttons appearing on the navigation item.

这样做之后,只需在 IB 设置中为一个插座分配一个工具栏(只需创建一个)并享受出现在导航项上的按钮。