ios 如何在导航栏的右侧添加多个 UIBarButtonItems?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30341263/
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 multiple UIBarButtonItems on right side of Navigation Bar?
提问by user1406716
I would like to have more than a single UIBarButtonItem
on the right side of my UINavigationBar
. How can I achieve this?
我想UIBarButtonItem
在我的右侧有多个单曲UINavigationBar
。我怎样才能做到这一点?
An example of what I am trying are shown below - you can notice that the top right has more than one button.
我正在尝试的示例如下所示 - 您可以注意到右上角有多个按钮。
回答by Dharmbir Singh
Use this in swift:
快速使用它:
override func viewDidLoad() {
super.viewDidLoad()
let editImage = UIImage(named: "plus")!
let searchImage = UIImage(named: "search")!
let editButton = UIBarButtonItem(image: editImage, style: .Plain, target: self, action: "didTapEditButton:")
let searchButton = UIBarButtonItem(image: searchImage, style: .Plain, target: self, action: "didTapSearchButton:")
navigationItem.rightBarButtonItems = [editButton, searchButton]
}
Write the action functions like this:
像这样编写动作函数:
func didTapEditButton(sender: AnyObject){
...
}
func didTapSearchButton(sender: AnyObject){
...
}
回答by Anoop
-(void)viewDidLoad{
UIBarButtonItem *anotherButton1 = [[UIBarButtonItem alloc] initWithTitle:@"Button_1" style:UIBarButtonItemStylePlain target:self action:@selector(button_1:)];
UIBarButtonItem *anotherButton2 = [[UIBarButtonItem alloc] initWithTitle:@"Button_" style:UIBarButtonItemStylePlain target:self action:@selector(button_2:)];
self.navigationItem.rightBarButtonItems=@[anotherButton1,anotherButton2];
}
回答by oscar castellon
In Swift 3 you can use:
在 Swift 3 中,您可以使用:
let editImage = UIImage(named: "plus")!
let searchImage = UIImage(named: "search")!
let editButton = UIBarButtonItem(image: editImage, style: .plain, target: self, action: #selector(didTapEditButton))
let searchButton = UIBarButtonItem(image: searchImage, style: .plain, target: self, action: #selector(didTapSearchButton))
navigationItem.rightBarButtonItems = [editButton, searchButton]
回答by Mr.Javed Multani
Simply add this code:
只需添加以下代码:
self.navigationItem.leftBarButtonItem = nil
let button = UIButton(type: .custom)
button.setImage(UIImage (named: "ChatTab"), for: .normal)
button.frame = CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0)
//button.addTarget(target, action: nil, for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: button)
let button2 = UIButton(type: .custom)
button2.setImage(UIImage (named: "ActivityTab"), for: .normal)
button2.frame = CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0)
//button.addTarget(target, action: nil, for: .touchUpInside)
let barButtonItem2 = UIBarButtonItem(customView: button2)
self.navigationItem.rightBarButtonItems = [barButtonItem, barButtonItem2]
This is the result:
这是结果:
回答by Christos Chadjikyriacou
I think nothing of the above is going to work
我认为以上都不起作用
Try this
尝试这个
var burgerItem = UIBarButtonItem(image: UIImage(named:"categories"), style: .Plain, target: self, action: "categories")
var weatherItem = UIBarButtonItem(title: "Weather", style: .Plain, target: self, action: "weather")
burgerItem.tintColor = UIColor.whiteColor()
weatherItem.tintColor = UIColor.whiteColor()
navigationItem.setRightBarButtonItems([burgerItem,weatherItem], animated: true)
You have to use navigationItem.setRightBarButtonItemsand be carefull. navigationItem has to be of a view controller.
您必须使用navigationItem.setRightBarButtonItems并小心。navigationItem 必须是一个视图控制器。
class testViewController:UIViewController {
ovverride func viewDidLoad() {
self.navigationItem.setRightBarButtonItems(...
}
}
回答by Kavin Varnan
With Swift 4
使用 Swift 4
let editImage = UIImage(named: "toolbar_edit")!
let favoriteImage = UIImage(named: "toolbar_fav_solid")!
let editButton = UIBarButtonItem(image: editImage, style: .plain, target: self, action: #selector(didTapEditButton))
let favoriteButton = UIBarButtonItem(image: favoriteImage, style: .plain, target: self, action: #selector(didTapFavoriteButton))
navigationItem.rightBarButtonItems = [editButton, favoriteButton]
Click action for those buttons
单击这些按钮的操作
@objc func didTapEditButton(sender: AnyObject) {
print("edit")
}
@objc func didTapFavoriteButton(sender: AnyObject) {
print("favorite")
}
Output screenshot (iPhone X)
输出截图(iPhone X)
回答by user2672052
Use navigationItem's rightBarButtonItems
, which takes an array of UIBarButton
. Appledoc
使用 navigationItem's rightBarButtonItems
,它接受一个UIBarButton
. 苹果文档
let share = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))
let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
navigationItem.rightBarButtonItems = [add, share]
回答by B.Saravana Kumar
This is may be help for you in objective-c,
这可能对你在Objective-c 中有所帮助,
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.navigationItem.title = @"Title";
UIBarButtonItem *logOutButton = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonItemStylePlain target:self action:@selector(ButtonClickedAtIndex1:)];
[logOutButton setTintColor:[UIColor blackColor]];
logOutButton.tag = 1;
UIBarButtonItem *syncBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Sync" style:UIBarButtonItemStylePlain target:self action:@selector(ButtonClickedAtIndex1:)];
[syncBarButtonItem setTintColor:[UIColor blackColor]];
syncBarButtonItem.tag = 2;
self.navigationItem.leftBarButtonItem = logOutButton;
self.navigationItem.rightBarButtonItem = syncBarButtonItem;
float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion >= 7.0) {
self.edgesForExtendedLayout = UIRectEdgeNone;
self.navigationController.navigationBar.translucent = NO;
}
}
return self;
}
回答by bjrne
I don't know wether an Interface Builder solution was not possible before, but at least with the most recent update (Xcode 11.0), it is possible to drag two UI Bar Buttons
into the rightBarButton space. Two outlets and actions can then be dragged into the code like always.
我不知道以前是否无法使用 Interface Builder 解决方案,但至少在最近的更新(Xcode 11.0)中,可以将两个UI Bar Buttons
拖入 rightBarButton 空间。然后可以像往常一样将两个出口和动作拖到代码中。
Since the OP didn't ask for a solution without IB, I think this also qualifies as an answer.
由于 OP 没有要求没有 IB 的解决方案,我认为这也可以作为答案。
回答by Shanu Singh
Accepted answer as well as few of the answer is absolutely correct but in my case none of them work.
接受的答案以及少数答案是绝对正确的,但就我而言,它们都不起作用。
Reason: My View Controller was Inherited from UIViewController and I had to set Add to Cart Button and Search Button On Right hand side. And All my view controllers were a part of UITabbarController.
原因:我的视图控制器是从 UIViewController 继承的,我必须在右侧设置添加到购物车按钮和搜索按钮。我所有的视图控制器都是 UITabbarController 的一部分。
let editImage = UIImage(named: "OrdersTabIcon")
let searchImage = UIImage(named: "OrdersTabIcon")
let editButton = UIBarButtonItem(image: editImage, style: .plain, target: self, action: #selector(iconTapped))
let searchButton = UIBarButtonItem(image: searchImage, style: .plain, target: self, action: #selector(iconTapped))
Instead of using
而不是使用
self.navigationItem.setRightBarButtonItems([editButton, searchButton], animated: true)
Use This .
用这个 。
self.navigationController?.navigationBar.topItem?.setRightBarButtonItems([editButton, searchButton], animated: true)