Skip to content

Instantly share code, notes, and snippets.

@wasnot
Last active August 29, 2015 14:23
Show Gist options
  • Save wasnot/01dd61b7057ac1cbf3a7 to your computer and use it in GitHub Desktop.
Save wasnot/01dd61b7057ac1cbf3a7 to your computer and use it in GitHub Desktop.
[iOS][Swift]Storyboardでボタン背景色の設定がしたい ref: http://qiita.com/wasnot/items/3537d932e9550a5c4c27
class BackgroundHighlightedButton: UIButton {
@IBInspectable var highlightedBackgroundColor :UIColor?
@IBInspectable var nonHighlightedBackgroundColor :UIColor?
override var highlighted :Bool {
didSet {
if highlighted {
self.backgroundColor = highlightedBackgroundColor
}
else {
self.backgroundColor = nonHighlightedBackgroundColor
}
}
}
}
extension UIButton {
private func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
func setBackgroundColor(color: UIColor, forUIControlState state: UIControlState) {
self.setBackgroundImage(imageWithColor(color), forState: state)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment