简体   繁体   English

UIBarButtomItem图像突出显示

[英]UIBarButtomItem image highlighted

I'm trying to setup a different image (highlighted) when the user press the UIBarButtomItem with this code: 当用户使用以下代码按下UIBarButtomItem时,我试图设置其他图像(突出显示):

self.addButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"addButton"]
                                                  style:UIBarButtonItemStylePlain
                                                 target:self
                                                 action:@selector(addAlert:)];
[self.addButton setBackgroundImage:[UIImage imageNamed:@"addButtonHigh"]
                              forState:UIControlStateSelected
                            barMetrics:UIBarMetricsDefault];
self.navigationItem.rightBarButtonItem = self.addButton;

But it is not working. 但这是行不通的。

The button appears with the "addButton" image, but when it is pressed the "addButtonHigh" image doesn't appear. 该按钮与“ addButton”图像一起出现,但是当它被按下时,“ addButtonHigh”图像不会出现。

Thank you in advance, Victor 提前谢谢你,维克多

change UIControlState from UIControlStateSelected to UIControlStateHighlighted . 将UIControlState从UIControlStateSelected更改为UIControlStateHighlighted If you want to change the background image highlighted. 如果要更改突出显示的背景图像。 You need to change the UIControlState. 您需要更改UIControlState。

The following is the code snippet i test. 以下是我测试的代码段。 it works. 有用。

self.addButton = [[UIBarButtonItem alloc] initWithTitle:@"hello" style:UIBarButtonItemStylePlain target:self action:@selector(addAlert:)];

[self.addButton setBackgroundImage:[UIImage imageNamed:@"font_minus_32.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.addButton setBackgroundImage:[UIImage imageNamed:@"font_plus_32.png"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

self.navigationItem.rightBarButtonItem = self.addButton;

Also maybe the following code is the code you wanted. 另外,也许下面的代码是您想要的代码。

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:@"font_minus_32.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"font_plus_32.png"] forState:UIControlStateHighlighted];
[btn addTarget:self action:@selector(addAlert:) forControlEvents:UIControlEventTouchUpInside];
[btn sizeToFit];

self.addButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = self.addButton;
- (IBAction)buttonClicked:(id)sender 
{

UIImage *buttonImage = [UIImage imageNamed:@"home.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateHighlighted];

}

UIControlStateHighlighted Highlighted state of a control. UIControlStateHighlighted控件的突出显示状态。 A control enters this state when a touch enters and exits during tracking and when there is a touch up event. 当在跟踪过程中进入和退出触摸以及发生触摸事件时,控件将进入此状态。 You can retrieve and set this value through the highlighted property. 您可以通过突出显示的属性来检索和设置此值。

UIControlStateSelected Selected state of a control. UIControlStateSelected控件的选定状态。 For many controls, this state has no effect on behavior or appearance. 对于许多控件,此状态对行为或外观没有影响。 But other subclasses (for example, the UISegmentedControl class) may have different appearance depending on their selected state. 但是其他子类(例如,UISegmentedControl类)可能会根据其选择状态而具有不同的外观。 You can retrieve and set this value through the selected property. 您可以通过选定的属性检索和设置此值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM