tags:

views:

370

answers:

2

I have subclassed UIToolbar and within it have created a set of UIButtonItems, all added in order and nicely spaced with flexible spaces. I now want to toggle one of the UIButtonItems each time it is tapped (just like the shuffle button in iPod, white blue white blue. I have maintained a reference to the UIButtonItem

// interface UIButtonItem *_shuffleButton; // released in dealloc

// implementation UIImage *_shuffleButtonImage = [UIImage imageNamed:@"shuffle_icon_200x100.png"]; _shuffleButton = [[UIBarButtonItem alloc] initWithImage:_shuffleButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(shuffleButtonTapped)];

So now in shuffleButtonTapped I want to do something like _shuffleButton.highlighted = YES; or _shuffleButton.selected = YES;

but neither of these work.

I have searched high and low and after failing to find anything I am starting to think I am missing something, can anyone tell me what it might be?? Thanks.

A: 

if you just want to show wome simple images as a buttons, use UITabBar instead. It will make selection UI changes automatically.

Morion
Thanks for your answer, although I want the blue appearance of the image I don't want the faded square that surrounds a selected tab. I am fairly sure you cannot have one without the other in a Tabbar. Like I said the effect I am after is the same as the shuffle button in the iPod app, where the image turns blue. I think I might have to roll my own, unless anyone else has any insights.
macasas
I'm not sure, but you can try initWithCustomView method of UIBarButtonItem and then change this view after clicking the button
Morion
+1  A: 

Here is what I did to create my own navigationItem Button which is in fact a view and can be animated just like any other view.

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom ]; btn.frame = CGRectMake(0, 0, 30, 30.0); [btn setBackgroundColor:[UIColor colorWithRed: 0.3f green:0.3f blue:0.3f alpha:1.0f]];

[btn setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal]; [btn addTarget:viewController action:@selector(rightButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:btn]; viewController.navigationItem.rightBarButtonItem = customItem; [customItem release];

macasas