after tapping the 'record' BarButtonItem I would like to keep it programmatically highlighted until the recording is over. The highlighting graphics of iOS are very good, therefor I would like to remain or set that state.
Up to now I found 'setSelected' and 'setHighlighted' but these do not work on a UIBarButtonItem. Any suggestions on how to solve this? Thank you in advance, Koen.
setSelected
and setHighlighted
work fine on UIControls, but not UIBarButtonItems (which are not UIControls).
I'd recommend using UIBarButtonItem's - (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics
(documentation linked) method to change the background image to something that mimics highlighting.
You can also set a custom UIView on the item which also mimics highlighting (see the customView
property).
If you absolutely want to use the default graphics, you could initialize your button item as
UIBarButtonItem *toggleButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"MyButton"
style:UIBarButtonItemStyleBordered
target:someObject
action:@selector(doSomething:)];
and toggle it with
toggleButtonItem.style = (toggleButtonItem.style == UIBarButtonItemStyleBordered)
? UIBarButtonItemStyleDone : UIBarButtonItemStyleBordered;
You would also need to use the style property to read the current state.
BOOL isSelected = (toggleButtonItem.style == UIBarButtonItemStyleDone)
If you add a UIBarButtonItem
with a UIButton
backing it, you can just ask for the CustomView.
UIButton *button = (UIButton *)[self.barButtonItem customView];
[button setSelected:YES];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With