私はUIButton
を持っていて、さまざまな状態の背景色を追加するための拡張を作成しました。UIButtonハイライトされた状態または選択された状態の問題の背景色
私は、次のコードを使用しています:
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)
私の問題は、それが強調表示/選択状態のための適切な
UIButton
背景色とタイトルの色を拾っていないことです。
私は同じ問題を抱えていました。押された状態は、私が思った(選択された)ものの代わりに強調表示されます:S。 – MQoder