How to Create Radio Buttons and Checkbox in Swift (Ios)

How to create radio buttons and checkbox in swift (iOS)?

For Radio Buttons and CheckBoxes there is nothing that comes built in.

You can implement Checkboxes easily yourself. You can set an uncheckedImage for your button for UIControlStateNormal and a checkedImage for your UIControlStateSelected. Now on tap, the button will change its image and alternate between checked and unchecked image.

To use radio buttons, you have to keep an Array for all the buttons that you want to behave as radio buttons. Whenever a button is pressed, you need to uncheck all other buttons in the array.

For radio buttons you can use SSRadioButtonsController
You can create a controller object and add buttons array to it like

var radioButtonController = SSRadioButtonsController()
radioButtonController.setButtonsArray([button1!,button2!,button3!])

The main principle is something like this here.

Swift Radio Buttons - CheckBoxes - Swift 3

You can achieve that in several ways. One of the most trivial would be to:

  • Have a datasource. Most of the time an array.
  • Create an UIButton for each item in your datasource, and insert each button into another array. Each button would have a tag corresponding at the
    index of the array. Also set different images for selected state and normal.
  • Add an action for each button, with the target function being something like:

    func buttonPressed(sender:UIButton) {
    for button in buttons {
    button.isSelected = false
    // deselect your model datasource[button.tag]
    }

    sender.isSelected = true
    // select your model datasource[button.tag]
    }

I leave the abstraction / improvements / safety to you. The key point is just to use a collection of buttons, iterate through them, and select / deselect them accordingly, and not using a big if/else checking for the button tag everytime.

how to create radio buttons dynamically in swift?

let buttons = [UIButton]()
// create button1
let button1 = UIButton(frame: CGRect(x: posX, y: posY, width: 60, height: 20))
button1.setTitleColor(UIColor.blackColor(), forState: .Normal)
button1.setTitle("No", forState: .Normal)
button1.setImage(UIImage(named: "checkbox untick.png")!, forState: .Normal)
// if the selected button cannot be reclick again, you can use .Disabled state
button1.setImage(UIImage(named: "checkboxredtick.png")!, forState: .Selected)
button1.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
myStackview.addSubview(button1)
buttons.append(button1)
// create other buttons and add into buttons ...

func buttonAction(sender: UIButton!){
for button in buttons {
button.selected = false
}
sender.selected = true
// you may need to know which button to trigger some action
// let buttonIndex = buttons.indexOf(sender)
}

The best way to use checkbox - IOS swift

There are lots of Checkbox control or you do it by this simple way:

For Storyboard:

  1. Set your button's selected image:

    Sample Image

  2. Set your button's default image:

    Sample Image

For Programmatically:

btn_box.setBackgroundImage(UIImage(named: "box"), for: .normal)
btn_box.setBackgroundImage(UIImage(named: "checkBox"), for: .selected)

And in button action:

@IBAction func btn_box(sender: UIButton) { 
sender.isSelected = !sender.isSelected
}

Create checkbox button in swift ios programatically

Replace pressedCheckTickAction function with this -

@objc func pressedCheckTickAction(_ sender: UIButton) {

currentTagofCheckbox = sender.tag
print("this is the tag of selected checkbox \(String(describing: currentTagofCheckbox))")

sender.isSelected = !sender.isSelected
let uncheckedValue = "unchecked"
print("\(uncheckedValue)")

if sender.isSelected {
sender.setBackgroundImage(#imageLiteral(resourceName: "checkedblack"), for: .normal)
getCheckBoxTick = "true"
self.submittedCheckinArray[currentTagofCheckbox!-1] = "true"
print("this is the submitted array in checkbox tick\(submittedCheckinArray)")
} else {
sender.setBackgroundImage(#imageLiteral(resourceName: "uncheckedblack"), for:.normal)
getCheckBoxTick = "false"
self.submittedCheckinArray[currentTagofCheckbox!-1] = "false"
print("this is the submitted array in checkbox tick\(submittedCheckinArray)")
}
}


Related Topics



Leave a reply



Submit