|
//Checkbox |
|
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, for: UIControlState.normal) |
|
} else { |
|
self.setImage(uncheckedImage, for: UIControlState.normal) |
|
} |
|
} |
|
} |
|
|
|
override func awakeFromNib() { |
|
self.addTarget(self, action:#selector(buttonClicked(sender:)), for: UIControlEvents.touchUpInside) |
|
self.isChecked = false |
|
} |
|
|
|
func buttonClicked(sender: UIButton) { |
|
if sender == self { |
|
isChecked = !isChecked |
|
} |
|
} |
|
} |
|
|
|
//Radio Button |
|
import UIKit |
|
|
|
class RadioButton: UIButton { |
|
var alternateButton:Array<RadioButton>? |
|
|
|
override func awakeFromNib() { |
|
self.layer.cornerRadius = 5 |
|
self.layer.borderWidth = 2.0 |
|
self.layer.masksToBounds = true |
|
} |
|
|
|
func unselectAlternateButtons(){ |
|
if alternateButton != nil { |
|
self.isSelected = true |
|
|
|
for aButton:RadioButton in alternateButton! { |
|
aButton.isSelected = false |
|
} |
|
}else{ |
|
toggleButton() |
|
} |
|
} |
|
|
|
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { |
|
unselectAlternateButtons() |
|
super.touchesBegan(touches, with: event) |
|
} |
|
|
|
func toggleButton(){ |
|
self.isSelected = !isSelected |
|
} |
|
|
|
override var isSelected: Bool { |
|
didSet { |
|
if isSelected { |
|
self.layer.borderColor = Color.turquoise.cgColor |
|
} else { |
|
self.layer.borderColor = Color.grey_99.cgColor |
|
} |
|
} |
|
} |
|
} |
|
|
|
//You can init your radio buttons like this: |
|
|
|
override func awakeFromNib() { |
|
self.view.layoutIfNeeded() |
|
|
|
womanRadioButton.selected = true |
|
manRadioButton.selected = false |
|
} |
|
|
|
override func viewDidLoad() { |
|
womanRadioButton?.alternateButton = [manRadioButton!] |
|
manRadioButton?.alternateButton = [womanRadioButton!] |
|
} |