[英]UIButton background color for highlighted/selected state issue
I've a UIButton
and I've created an extension to add background color for different state.我有一个UIButton
并且我创建了一个扩展来为不同的状态添加背景颜色。
I'm using the following code:我正在使用以下代码:
extension UIButton {
func setBackgroundColor(color: UIColor, forState: UIControlState) {
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), color.CGColor)
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRect(x: 0, y: 0, width: 1, height: 1))
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.setBackgroundImage(colorImage, forState: forState)
}
}
// Set Button Title and background color for different states
self.donateButton.setBackgroundColor(UIColor.redColor(), forState: .Normal)
self.donateButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
self.donateButton.setBackgroundColor(UIColor.greenColor(), forState: .Highlighted)
self.donateButton.setTitleColor(UIColor.whiteColor(), forState: .Highlighted)
self.donateButton.setBackgroundColor(UIColor.greenColor(), forState: .Selected)
self.donateButton.setTitleColor(UIColor.whiteColor(), forState: .Selected)
My Problem is that it is not picking up proper
UIButton
background color and title color for highlighted/selected state.我的问题是它没有为突出显示/选定状态选择正确的UIButton
背景颜色和标题颜色。
我发现了这个问题, UIButton
被设置为System 。我只是将其更改为Custom ,它开始按预期工作。
Update Swift 4更新 Swift 4
extension UIButton {
func setBackgroundColor(color: UIColor, forState: UIControlState) {
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
UIGraphicsGetCurrentContext()!.setFillColor(color.cgColor)
UIGraphicsGetCurrentContext()!.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.setBackgroundImage(colorImage, for: forState)
}
}
Event Button Action Show Touch On Highlight事件按钮动作显示触摸高亮
Swift 5, IOS 13+斯威夫特 5, IOS 13+
extension UIButton {
func setBackgroundColor(_ color: UIColor, for forState: UIControl.State) {
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
UIGraphicsGetCurrentContext()!.setFillColor(color.cgColor)
UIGraphicsGetCurrentContext()!.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.setBackgroundImage(colorImage, for: forState)
}
}
The reason it does not work is that you have not set the correct state: you are missing the [ .highlighted, .selected ]
state, and so it falls back to plain .normal
.它不起作用的原因是您没有设置正确的状态:您缺少[ .highlighted, .selected ]
状态,因此它回落到普通的.normal
。
When both isSelected
and isHighlighted
are true, UIKit looks explicitly for the state-combination of the two, and will not accept any of them individually.当isSelected
和isHighlighted
都为 true 时,UIKit 会明确查找两者的状态组合,并且不会单独接受它们中的任何一个。
The original code extended with the missing state (and updated to valid syntax in Swift 5):原始代码扩展了缺少的状态(并在 Swift 5 中更新为有效语法):
self.donateButton.setBackgroundColor(.green, for: [ .highlighted, .selected ])
self.donateButton.setTitleColor(.white, for: [ .highlighted, .selected ])
When your UIButton
is selected.当您的UIButton
被选中时。 If you focus on the button, it will not highlight the button as state .Highlighted
.如果您关注按钮,它不会将按钮突出显示为 state .Highlighted
。 If it is not selected, it will highlight it as your will.如果它没有被选中,它会根据你的意愿突出显示它。 For it regards you have background image in your button, it will use the default effect to highlight a image.因为它认为您的按钮中有背景图像,它将使用默认效果来突出显示图像。 If you want to change the state to .Selected
, you have to set the variable selected
of UIButton
to true.如果要将状态更改为.Selected
,则必须将UIButton
selected
变量设置为 true。
要实现自定义按钮照明,您需要: - 在故事板中设置您想要看到的背景颜色和 alpha,当按钮被按下时 - 在初始化时,将背景和 alpha 设置为未按下的按钮 - 实施按钮的三种方法(Touch Down / Touch Up Side / Touch Drag Outside) - 在其中,更改按钮照明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.