UIToolbar没有显示UIBarButtonItem [英] UIToolbar not showing UIBarButtonItem

查看:144
本文介绍了UIToolbar没有显示UIBarButtonItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用故事板,我有一个拆分视图,其中master是一个UITableViewController。就像iPad Mail应用程序一样,我想显示一个UIToolbar。

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.

我无法通过故事板添加工具栏但我设法以编程方式添加它。我也可以在工具栏中添加UILabel,但我找不到添加刷新按钮或任何UIBarButtonItem的方法。

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.

任何想法?

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


推荐答案

感谢Apple iOS论坛找到答案!

Found the answer thanks to the Apple iOS Forum!

当您使用导航控制器的工具栏时,您必须在活动视图控制器的 toolbarItems上设置工具栏按钮属性,不在实际导航控制器的工具栏本身上。

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.

来自 UINavigationController docs

From the UINavigationController docs:


显示工具栏

导航控制器对象在其视图
层次结构中管理可选工具栏。显示时,此工具栏从活动视图控制器的 toolbarItems 属性中获取其当前的
项目集。
当活动视图控制器更改时,导航控制器
更新工具栏项以匹配新视图控制器,在适当时将新项目设置为

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.

例如:

[self setToolbarItems:buttons animated:NO];

这篇关于UIToolbar没有显示UIBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆