For a checkbox, you don't need to subclass the UIButton. It already has the isSelected
property to handle this.
checkbox = UIButton.init(type: .custom)
checkbox.setImage(UIImage.init(named: "iconCheckboxOutlined"), for: .normal)
checkbox.setImage(UIImage.init(named: "iconCheckboxFilled"), for: .selected)
checkbox.addTarget(self, action: #selector(self.toggleCheckboxSelection), for: .touchUpInside)
Then in the action method toggle it's isSelected
state.
@objc func toggleCheckboxSelection() {
checkbox.isSelected = !checkbox.isSelected
}