【问题标题】:barButtonItem in UIToolBar of NavigationController not working in ios7NavigationController的UIToolBar中的barButtonItem在ios7中不起作用
【发布时间】:2014-10-06 15:10:03
【问题描述】:

我正在尝试在 NavigationController 的 UIToolBar 中添加 BarButton。我在堆栈溢出的以下代码中找到了。但它不适用于ios7。如果有任何改变,为什么它在工具栏上什么也不显示。

 [self.navigationController setToolbarHidden:NO];


 UIBarButtonItem *buttonItem = [[ UIBarButtonItem alloc ] initWithTitle: @"Select All"
                                            style: UIBarButtonItemStyleBordered
                                           target: self
                                           action: @selector(selectAll:) ];
  UIBarButtonItem *buttonNext = [[UIBarButtonItem alloc]initWithTitle:@"Next"     style:UIBarButtonItemStyleBordered target:self action:@selector(goNext:)];
   self.toolbarItems = [ NSArray arrayWithObjects: buttonItem, buttonNext, nil ];

【问题讨论】:

  • 您需要将其添加到视图中。 [self.view addSubview:self.toolbarItems]
  • 不需要添加这个来查看。因为这是导航控制器的默认属性。我们只需要将ToolbarHidden 属性设置为NO。我的代码现在正在运行。通过删除我的视图控制器中的功能。覆盖 self.toolbarItems = [ NSArray arrayWithObjects: buttonItem, buttonNext, nil ];
  • 以上代码在 ios7 中运行。这是我的错。没有工作正常。
  • 好的,没问题。很酷,它正在工作

标签: ios7 uinavigationcontroller uibarbuttonitem uitoolbar toolbaritems


【解决方案1】:
//allocate the tool bar 
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 416, 320, 44)];

//  create bar btns
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(Back)];

//set spacing
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

UIBarButtonItem *forward = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(Forward)];

// add btns to the bar
[toolBar setItems:[NSMutableArray arrayWithObjects:back,flexibleSpace,forward, nil]];

// adds the toobar to the view
[self.view addSubview:toolBar];

【讨论】:

  • 下次请正确格式化您的代码并尝试为您的答案添加一些解释。
【解决方案2】:

我发现了问题。上面的代码是正确的。工具栏中没有显示按钮的原因。我的课上也有这个功能。这是一个空函数。所以它覆盖了默认的 self.toolbarItems = [ NSArray arrayWithObjects: buttonItem, buttonNext, nil ]; 删除以下代码后。它现在工作完美。

 - (void)setToolbarItems:(NSArray *)toolbarItems animated:(BOOL)animated
 { 

 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多