NavigationController的UIToolBar中的barButtonItem在ios7中不起作用

时间:2014-08-13 09:49:48

标签: ios7 uinavigationcontroller uibarbuttonitem uitoolbar toolbaritems

我正在尝试在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 ];

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

 }