UIToolbar not showing UIBarButtonItem

余生长醉 提交于 2019-12-03 10:58:00

问题


I'm using a storyboard and I've a split view where the master is a UITableViewController. Like the iPad Mail app, I'd like to display a UIToolbar.

I wasn't able to add the toolbar via storyboard but I managed to add it programmatically. I'm also able to add a UILabel to the toolbar, but I can't find a way to add a refresh button or any kind of UIBarButtonItem.

Any idea?

- (void)viewDidLoad {
  [super viewDidLoad];

  [self.navigationController setToolbarHidden:NO];

  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50.0f, 0.0f, 80.0f, 40.0f)];
  label.text = @"last updated...";
  label.textAlignment = UITextAlignmentCenter;
  label.font = [UIFont systemFontOfSize:13.0];
  [self.navigationController.toolbar addSubview:label];

  UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStylePlain target:self action:@selector(action:)];
  UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"Item1" style:UIBarButtonItemStyleBordered target:self action:@selector(action:)];

  NSArray *buttons = @[item1, item2, nil];
  [self.navigationController.toolbar setItems:buttons animated:NO];

回答1:


Found the answer thanks to the Apple iOS Forum!

When you use the toolbar of the navigation controller, you have to set the toolbar buttons on the active view controller's toolbarItems property, not on the actual navigation controller's toolbar itself.

From the UINavigationController docs:

Displaying a Toolbar

A navigation controller object manages an optional toolbar in its view hierarchy. When displayed, this toolbar obtains its current set of items from the toolbarItems property of the active view controller. When the active view controller changes, the navigation controller updates the toolbar items to match the new view controller, animating the new items into position when appropriate.

For example:

[self setToolbarItems:buttons animated:NO];


来源:https://stackoverflow.com/questions/10825572/uitoolbar-not-showing-uibarbuttonitem

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