ios 如何在单击时禁用 UIButton 突出显示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17528397/
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 UIButton highlight on click?
提问by Centurion
How to disable button highlight effect on click? Have custom button with white bg color and DarkGray text color. The problem is the text becomes white on button click. Already tried but none of them worked:
如何在单击时禁用按钮高亮效果?具有白色背景颜色和深灰色文本颜色的自定义按钮。问题是单击按钮时文本变为白色。已经尝试过,但没有一个奏效:
a. Did uncheck "Highlighted Ajusts Image" in interface builder.
一种。在界面构建器中取消选中“突出显示的调整图像”。
b. Tried setting highlighted = NO in button pressed method:
湾 在按下按钮的方法中尝试设置突出显示 = NO:
((UIButton *)sender).highlighted = NO
c. Tried setting the same title for highlihted state:
C。尝试为高亮状态设置相同的标题:
[button setTitle:[button titleForState:UIControlStateNormal] forState:UIControlStateHighlighted];
Any suggestions?
有什么建议?
采纳答案by LE SANG
UIButton will highlighted on click, so check button setting Change the title color in highlight state config to same as default state Or you can set:
UIButton 将在单击时突出显示,因此请检查按钮设置将突出显示状态配置中的标题颜色更改为与默认状态相同,或者您可以设置:
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
If you want to control Highlighted by code, you can disable normal highlighted by subclass Button and disable in touchesBegin:
如果你想通过代码来控制高亮,你可以禁用子类Button的正常高亮并在touchesBegin中禁用:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if (self.state == UIControlStateHighlighted) {
[self setHighlighted:NO];
}
}
回答by Paul Brewczynski
You could also make the UIButton type : custom from StoryBoard
您还可以制作 UIButton 类型:来自 StoryBoard 的自定义
This should strip down any system behavior for you. (I think)
这应该为您剥离任何系统行为。(我认为)
回答by Rajeev Bhatia
This is a solution for buttons that have images. In the storyboard, there is a property called HighlightedAdjustsImage
which is On by default. Disable that and your button image will not show any highlighted properties.
这是具有图像的按钮的解决方案。在故事板中,有一个名为的属性HighlightedAdjustsImage
,默认情况下为 On。禁用它,您的按钮图像将不会显示任何突出显示的属性。
回答by ibrahimyilmaz
回答by Jeremie D
old outdated answers, and old question, but the newest and easiest way is:
旧的过时答案和旧问题,但最新和最简单的方法是:
overlayButton.adjustsImageWhenHighlighted = false
回答by ephemer
None of the solutions I found online worked for me (including these ones listed here), so I ended up doing this in my custom UIButton
subclass:
我在网上找到的所有解决方案都不适合我(包括此处列出的这些),因此我最终在我的自定义UIButton
子类中执行了此操作:
// swift:
override var isHighlighted: Bool {
didSet { if isHighlighted { isHighlighted = false } }
}
I also have the buttonType
set to .custom
, but it probably makes no difference here.
我也有buttonType
设置为.custom
,但在这里可能没有区别。
And now: no more weird highlighting or colour animations!
现在:不再有奇怪的突出显示或彩色动画!
回答by AamirR
My 2 cents ;) Swift 3 ready
我的 2 美分 ;) Swift 3 准备好了
class CustomButton: UIButton {
override var isHighlighted: Bool {
didSet {
if (isHighlighted) {
super.isHighlighted = false
}
}
}
}
Situation and History
情况和历史
I had set images for UIControlState.normal
and UIControlState.selected
, so setting UIButton.isSelected = true
shows selected image, else the normal image.
我已经为UIControlState.normal
和设置了图像UIControlState.selected
,因此设置UIButton.isSelected = true
显示所选图像,否则显示正常图像。
The issue we discovered was when UIButton.isSelected = true
, and during highlight/focus (by tap hold the button and discard tap by swiping away)button shows image for UIControlState.normal
, that looked pretty ugly to my boss :D
我们发现的问题是UIButton.isSelected = true
在高亮/聚焦期间(通过点击按住按钮并通过滑动放弃点击)按钮显示 的图像UIControlState.normal
,这对我的老板来说看起来很丑陋:D
I tried several solutions, such as UIButton(type: UIButtonType.custom)
and UIButton.adjustsImageWhenHighlighted = false
, none helped :(
我尝试了几种解决方案,例如UIButton(type: UIButtonType.custom)
and UIButton.adjustsImageWhenHighlighted = false
,都没有帮助:(
回答by iOS Lifee
You can do it by creating extension in Swift 4.2
您可以通过在 Swift 4.2 中创建扩展来实现
extension UIButton {
open override var isHighlighted: Bool {
didSet {
super.isHighlighted = false
}
}}