Skip to content

Instantly share code, notes, and snippets.

@uy
Last active September 28, 2021 15:33
Show Gist options
  • Save uy/3d9f4bd9c28063416f9da42425bb9430 to your computer and use it in GitHub Desktop.
Save uy/3d9f4bd9c28063416f9da42425bb9430 to your computer and use it in GitHub Desktop.
Custom Checkbox iOS Swift
import UIKit
class CheckBox: UIButton {
// Images
let checkedImage = UIImage(named: "ic_check_box")! as UIImage
let uncheckedImage = UIImage(named: "ic_check_box_outline_blank")! as UIImage
// Bool property
var isChecked: Bool = false {
didSet{
if isChecked == true {
self.setImage(checkedImage, forState: .Normal)
} else {
self.setImage(uncheckedImage, forState: .Normal)
}
}
}
override func awakeFromNib() {
self.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
self.isChecked = false
}
func buttonClicked(sender: UIButton) {
if sender == self {
isChecked = !isChecked
}
}
}
@uy
Copy link
Author

uy commented Feb 24, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment