首页 文章

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

提问于
浏览
10

我有标准的UIBarButtonItem

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

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

self.toolbarItems = [NSArray arrayWithObject:share];

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

4 回答

  • 1

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

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

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

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

    IBOutlet UIToolbar *toolBar;
    
    UIBarButtonItem *infoButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"info" style:UIBarButtonItemStyleBordered  target:self action:@selector(infoButtonClicked)];
    
    toolBar.items = [NSArray arrayWithObjects:infoButtonItem, nil];
    
  • 9

    确保工具栏不是't hidden; you could try adding the following to your view controller' s viewWillAppear:animated: 方法:

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

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

相关问题