简体   繁体   中英

How to add UIBarButtonItem in UIToolBar in code

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.

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;

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];

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];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM