【问题标题】:How can I change the UIButton "glow" color used when "Shows Touch On Highlight" is enabled?如何更改启用“高亮显示触摸”时使用的 UIButton“发光”颜色?
【发布时间】:2018-04-30 05:49:45
【问题描述】:
我有一个 UIButton,在情节提要中启用了“在突出显示时显示触摸”。单击时它会向按钮应用“白色”光晕。
但是,我有一个浅灰色按钮,我想更改发光颜色以使其更明显。
我尝试将突出显示的控件状态的背景图像设置为黑色,但它似乎与发光效果没有什么不同。我可以更改按钮的背景颜色——一切正常。
什么控制发光颜色,如果有的话?或者如果我需要不同的颜色,我必须手动完成吗?
【问题讨论】:
标签:
ios
xamarin
uibutton
touch
highlight
【解决方案1】:
“在突出显示时显示触摸”纯粹是一种便利,因此如果您想要不同的效果,您必须手动执行操作。只需为突出显示的状态提供不同的图像,其中包含所需的“发光”绘图。
【解决方案2】:
在UIButton 上使用setBackgroundImage,您可以创建以下扩展程序,让您可以为所需的任何状态设置颜色:
extension UIButton {
func setBackgroundColor(color: UIColor, for state: UIControlState) {
let image = UIImage.image(with: color)
let insets = UIEdgeInsetsMake(0, 0, 0, 0)
let stretchable = image.resizableImage(withCapInsets: insets, resizingMode: .tile)
self.setBackgroundImage(stretchable, for: state)
}
}
您只需要为.highlighted 状态设置背景颜色:
button.setBackgroundColor(color: UIColor.red, for: .highlighted)
我只是希望你不介意用代码弄脏你的手,我不太确定如何从情节提要中做同样的事情(我什至不确定你能做到)。