ios 如何在代码中的 UIToolBar 中添加 UIBarButtonItem

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

How to add UIBarButtonItem in UIToolBar in code

objective-cxcodeiosuibarbuttonitem

提问by Tunyk Pavel

I have standart UIBarButtonItem

我有标准的 UIBarButtonItem

UIBarButtonItem *share = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(share:)];

How to add her to UIToolBar? I've tried

如何将她添加到 UIToolBar?我试过了

    self.toolbarItems = [NSArray arrayWithObject:share];

But it doesn't work. Need your help.

但它不起作用。需要你的帮助。

回答by Caleb

Can you be more specific than "it doesn't work"?

你能比“它不起作用”更具体吗?

If you're trying to add an item to a toolbar that already has items, you'll need to modify the array of items:

如果您尝试向已有项目的工具栏添加项目,则需要修改项目数组:

NSMutableArray *newItems = [self.toolbarItems mutableCopy];
[newItems addObject:share];
self.toolbarItems = newItems;

回答by Gypsa

Make sure you have make a toolbar either an IBOutlet or added toolbar programatically

确保您已将工具栏设为 IBOutlet 或以编程方式添加工具栏

IBOutlet UIToolbar *toolBar;

UIBarButtonItem *infoButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"info" style:UIBarButtonItemStyleBordered  target:self action:@selector(infoButtonClicked)];

toolBar.items = [NSArray arrayWithObjects:infoButtonItem, nil];

回答by conmulligan

Make sure the toolbar isn't hidden; you could try adding the following to your view controller's viewWillAppear:animated:method:

确保工具栏没有隐藏;您可以尝试将以下内容添加到您的视图控制器的viewWillAppear:animated:方法中:

[self.navigationController setToolbarHidden:NO animated:YES];

回答by Kobski

[toolbar setItems:[NSArray arrayWithObject:share] animated:YES];

[toolbar setItems:[NSArray arrayWithObject:share] animated:YES];