开发者

iPhone UIToolbar UIButtonItem highlighting

开发者 https://www.devze.com 2022-12-13 13:17 出处:网络
开发者_运维百科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 ea
开发者_运维百科

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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消