Alberto Mier Alberto Mier - 13 days ago 5
Swift Question

Make CheckBox in swift

Is it possible to achieve this using

swift
?

enter image description here

I would like to make many checkboxes inside an
UIView
in
swift

Answer

Simple just check for the image loaded on button and take appropriate action see below code:

// declare bool
var unchecked = true 

@IBAction func tick(sender: UIButton) {
    if unchecked {
        sender.setImage(UIImage(named:"checked.png"), forControlState: .Normal)
        unchecked = false
    }
    else {
        sender.setImage( UIImage(named:"unchecked.png"), forControlState: .Normal)
        unchecked = true
    }
}

Note:

  • You need to use two different images named as checked and unchecked.
  • Then above code is used for separate button (checkmarks) you need to create.