How to add UIBarButtonItem in UIToolBar in code

2019-06-16 04:48发布

I have standart UIBarButtonItem

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

How to add her to UIToolBar? I've tried

    self.toolbarItems = [NSArray arrayWithObject:share];

But it doesn't work. Need your help.

4条回答
劳资没心,怎么记你
2楼-- · 2019-06-16 05:29

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;
查看更多
乱世女痞
3楼-- · 2019-06-16 05:40

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

查看更多
神经病院院长
4楼-- · 2019-06-16 05:44

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

IBOutlet UIToolbar *toolBar;

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

toolBar.items = [NSArray arrayWithObjects:infoButtonItem, nil];
查看更多
我命由我不由天
5楼-- · 2019-06-16 05:49

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

[self.navigationController setToolbarHidden:NO animated:YES];
查看更多
登录 后发表回答