barButtonItem in UIToolBar of NavigationController not working in ios7

465 Views Asked by At

I am trying to add BarButton in UIToolBar of NavigationController. I found on below code on stack over flow. But it is not working in ios7. If anything changed why it shows nothing on tool bar.

 [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

There are 2 best solutions below

1
On
//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];
0
On

I found the problem. Above code is correct. The reason why there is no button showing in the tool bar . i also have this function in my class. this is a empty function. so it overwrite the default self.toolbarItems = [ NSArray arrayWithObjects: buttonItem, buttonNext, nil ]; After Removing the below code. Its working Perfect Now.

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

 }