Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cheebow
Created July 3, 2015 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cheebow/4876dfbbdde6724fd249 to your computer and use it in GitHub Desktop.
Save cheebow/4876dfbbdde6724fd249 to your computer and use it in GitHub Desktop.
ColorButton
import UIKit
class ColorButton: UIButton {
func setBackgroundColor(color: UIColor, forState state: UIControlState) {
self.setBackgroundImage(self.colorImage(color), forState: state)
}
func colorImage(color: UIColor)-> UIImage {
let size = CGSizeMake(1.0, 1.0)
let rect = CGRect(origin: CGPointZero, size: size)
UIGraphicsBeginImageContext(size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment