ios 如何禁用 UIButton 的高亮控件状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2259905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable the highlight control state of a UIButton?
提问by kbanman
I've got a UIButton that, when selected, shouldn't change state when being touched. The default behaviour is for it to be in UIControlStateHighlighted while being touched, and this is making me angry.
我有一个 UIButton,当它被选中时,被触摸时不应改变状态。默认行为是它在被触摸时处于 UIControlStateHighlighted 中,这让我很生气。
Suggestions?
建议?
回答by Haydn
Your button must have its buttonType
set to Custom.
您的按钮必须buttonType
设置为自定义。
In IB you can uncheck "Highlight adjusts image".
在 IB 中,您可以取消选中“突出显示调整图像”。
Programmatically you can use theButton.adjustsImageWhenHighlighted = NO;
您可以以编程方式使用 theButton.adjustsImageWhenHighlighted = NO;
Similar options are available for the "disabled" state as well.
类似的选项也可用于“禁用”状态。
回答by Mosib Asad
In addition to above answer of unchecking "highlight adjusts image" in IB, make sure that button type is set CUSTOM.
除了上面在IB中取消选中“突出显示调整图像”的答案外,请确保按钮类型设置为CUSTOM。
回答by Manish Ahuja
This will work for you:
这对你有用:
[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];
3rd line is the trick here...
第三行是这里的诀窍......
This works the same for setting image/backgroundImage
这与设置图像/背景图像相同
回答by KETAN
adjustsImageWhenHighlighted = NO;
回答by Tim
button.adjustsImageWhenDisabled = NO;
is equally useful for having your own appearance of a disabled button.
对于拥有自己的禁用按钮外观同样有用。
回答by Dimitris
Depending on what changes from the default to the highlighted state of the button, you can call a couple of methods to set them to what you need. So if the image changes you can do
根据按钮从默认状态到突出显示状态的变化,您可以调用几个方法来将它们设置为您需要的状态。因此,如果图像发生变化,您可以这样做
[myButton setImage:[myButton imageForState:UIControlStateNormal] forState:UIControlStateHighlighted];
If the text changes you can do
如果文本更改,您可以执行
[myButton setTitle:[myButton titleForState:UIControlStateNormal] forState:UIControlStateHighlighted];
other similar functions:
其他类似功能:
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state
回答by therealbuckley
OK here's an easy solution if this works for you, after a week of banging my head on this it finally occurred to me to just set highlighted=NO for the 1st line of the IBAction method for the TouchUpInside or TouchDown, or whatever works. For me it was fine on the TouchUpInside.
好的,如果这对您有用,这是一个简单的解决方案,经过一周的努力,我终于想到只需为 TouchUpInside 或 TouchDown 的 IBAction 方法的第一行设置突出显示 = NO,或任何有效的方法。对我来说,TouchUpInside 很好。
-(IBAction)selfDismiss:(id)sender {
self.btnImage.highlighted = NO;
NSLog(@"selfDismiss");
etc, etc, etc.
}
回答by iDeveloper
For Swifty Developer -
对于 Swifty 开发人员 -
yourButton.adjustsImageWhenHighlighted = false
回答by yuanjilee
Swift 3+
斯威夫特 3+
button.adjustsImageWhenHighlighted = false
button.adjustsImageWhenDisabled = false
回答by Khushboo
make your button Type - "Custom" and Uncheck - Highlighted Adjust image and you are done.
使您的按钮类型 - “自定义”和取消选中 - 突出显示调整图像,您就完成了。