Search code examples
swiftswift2swift3

Make CheckBox in swift for IOS


Is it possible to achieve this using swift?

enter image description here

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


Solution

  • 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.