How to add UIBarButtonItem in UIToolBar in code

橙三吉。 提交于 2019-12-08 15:40:45

问题


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.


回答1:


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;



回答2:


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



回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/5383906/how-to-add-uibarbuttonitem-in-uitoolbar-in-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!