禁用UIButton的突出显示的UIControl状态

时间:2011-05-03 10:59:31

标签: ios cocoa-touch uibutton

我有UIButton,如果按钮处于选中状态,我想禁用其UIControlStateHighlighted。我的意思是,如果UIButton的当前状态为ControlStateSelected,那么在触摸时,其状态不应更改为突出显示哪个是UIButton的默认行为。

4 个答案:

答案 0 :(得分:18)

[button setBackgroundImage:[UIImage imageNamed:@"button_image"]forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"button_image_selected"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:@"button_image_selected"] forState:UIControlStateSelected | UIControlStateHighlighted];

第三行是这里的技巧,如果按钮已经处于选中状态,它将禁用UIButton的突出显示状态

答案 1 :(得分:9)

取消选中IB中的“高亮调整图像”,同时确保按钮类型设置为IB中的CUSTOM

答案 2 :(得分:3)

if(button.selected == YES)
button.adjustsImageWhenHighlighted = NO;
else
button.adjustsImageWhenHighlighted = YES;

希望这有帮助

答案 3 :(得分:1)

只有两件事:

UIButton *btnTransparentComponent = [UIButton buttonWithType:UIButtonTypeCustom];
btnTransparentComponent.adjustsImageWhenHighlighted = NO;