如何在iOS 7中设置UIBarButtonItem选中或突出显示的图像或色调? [英] how to set UIBarButtonItem selected or highlighted image or tint colours in iOS 7?

查看:328
本文介绍了如何在iOS 7中设置UIBarButtonItem选中或突出显示的图像或色调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iOS 7中向uibarbuttonitem提供正常状态和选中/突出显示的状态图像?有没有办法为uibarbuttonitem的正常和选定/突出显示状态提供色调颜色?

How to provide normal state and selected/highlighted state images to uibarbuttonitem in iOS 7? Is there any way to provide tint colour for both normal and selected/highlighted state of uibarbuttonitem?

我不想使用uibutton作为uibarbuttonitem的视图!任何优雅的解决方案都将受到高度赞赏。

I don't want to use uibutton as a view for uibarbuttonitem! Any elegant solution would be highly appreciated.

推荐答案

您可以使用 UIButton 作为一个 UIBarButtonItem customView ,并且有一个正常的两个不同的背景图像选择 UIButton的状态。然后,当点击按钮时,您只需将选择的状态设置为 YES

You can use a UIButton as a customView of a UIBarButtonItem and have two different background images for a normal and selected state of the UIButton. Then when the button is tapped, you can just set the selected state to YES or NO.

// Build right bar button
UIImage *normalButtonImage = [UIImage imageNamed:@"normal-button"];
UIImage *selectedButtonImage = [UIImage imageNamed:@"selected-button"];
CGRect rightButtonFrame = CGRectMake(0, 0, normalButtonImage.size.width,
                                           normalButtonImage.size.height);
UIButton *rightButton = [[UIButton alloc] initWithFrame:rightButtonFrame];
[rightButton setBackgroundImage:normalButtonImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:selectedButtonImage forState:UIControlStateSelected];
[rightButton addTarget:self action:@selector(rightBarButtonPress)
         forControlEvents:UIControlEventTouchDown];
[orientationButton setShowsTouchWhenHighlighted:YES];
[orientationButton setSelected:NO];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
[self.navigationItem setRightBarButtonItem:rightBarButton];

然后点击按钮时的in方法,更改状态

Then your in method for when the button is clicked, change the state

- (void)rightBarButtonPress
{
    //toggle selected state of button
    UIBarButtonItem *rightBarButton = self.navigationItem.rightBarButtonItem;
    UIButton *button = (UIButton *)rightBarButton.customView;
    [button setSelected:!button.selected];

    //do whatever else you gotta do
}

这篇关于如何在iOS 7中设置UIBarButtonItem选中或突出显示的图像或色调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆