【发布时间】:2018-02-26 09:45:45
【问题描述】:
我希望按钮在突出显示时具有白色背景和蓝色标题。我有一个 UIButton 的扩展来设置它的背景颜色。
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)
self.clipsToBounds = true
}
}
在下一个函数中,我设置了一个特定的按钮。
private func stylizingButton(button: UIButton){
button.layer.borderWidth = 2
button.layer.borderColor = textColor.cgColor
button.layer.cornerRadius = 8
button.setTitleColor(textColor, for: .normal)
button.setTitleColor(backgroundColor, for: .highlighted)
button.setBackgroundColor(color: .white, forState: .highlighted)
}
当我将按钮的背景颜色更改为黑色时,结果是一些深蓝色。就像屏幕背景颜色和按钮颜色混合在一起一样。
【问题讨论】:
-
您是否也使用
setBackgroundColor将其设置为黑色? -
是的,然后当它突出显示时我会变成深蓝色