UIBarButtonItem does not highlight when displayed if on toolbar?

I need two buttons on the left side of the navigation bar. The only way I figured out how to do this is to put them in the UIToolbar first and then set the leftBarButtonItem element for it.

If I do this, it works fine (you can see that it stands out when pressed):

UIBarButtonItem* myBtn = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomething:)]; self.navigationItem.leftBarButtonItem = myBtn; 

But if I do this, the action of the button still happens, but there is no selection (there is no visual feedback that you click on the button):

  NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2]; UIBarButtonItem* myBtn = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomething:)]; UIBarButtonItem* myBtn2 = [[UIBarButtonItem alloc] initWithTitle:@"Button2" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomethingElse:)]; [buttons addObject:myBtn]; [buttons addObject:myBtn2]; UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44)]; [toolbar setItems:buttons animated:NO]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar]; 

Any idea why this causes the buttons to not stand out when touched?

+4
source share
2 answers

I do not think that the UIBarButtonItem object will be highlighted when touched. Even for the default back button, touch highlighting is added to the navigation bar. It only works that way. Not sure, but you can try using a UISegmentedControl with a single segment. This can create a highlighted illusion and will only look like a barbate.

0
source

In your select function (for example, doSomething ), set the tintColor property of the button to the desired highlighted color. And make sure that the highlight color of your button is different from the default color so that you know that it is highlighted.

According to Apple Docs , the showsTouchWhenHighlighted function has the form:

A Boolean value that determines whether a button presses a button.

but this is only for UIButton. I do not believe that UIBarButtonItem has a selection option.

0
source

All Articles