Checkbox in iOS Application

How to create a simple checkbox in iOS?

Yeah, no checkbox for you in iOS (-:

Here, this is what I did to create a checkbox:

UIButton *checkbox;
BOOL checkBoxSelected;
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(x,y,20,20)];
// 20x20 is the size of the checkbox that you want
// create 2 images sizes 20x20 , one empty square and
// another of the same square with the checkmark in it
// Create 2 UIImages with these new images, then:

[checkbox setBackgroundImage:[UIImage imageNamed:@"notselectedcheckbox.png"]
forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:@"selectedcheckbox.png"]
forState:UIControlStateSelected];
[checkbox setBackgroundImage:[UIImage imageNamed:@"selectedcheckbox.png"]
forState:UIControlStateHighlighted];
checkbox.adjustsImageWhenHighlighted=YES;
[checkbox addTarget:(nullable id) action:(nonnull SEL) forControlEvents:(UIControlEvents)];
[self.view addSubview:checkbox];

Now in the target method do the following:

-(void)checkboxSelected:(id)sender
{
checkBoxSelected = !checkBoxSelected; /* Toggle */
[checkbox setSelected:checkBoxSelected];
}

That's it!

Checkbox control for iOS application

You can use the selected state of a UIButton, set different images (or also texts) for differents (via code, or Interface Builder) :

[button setImage:[UIImage imageNamed:@"selected.png"]
forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:@"unselected.png"]
forState:UIControlStateNormal];

And in the touch up inside action :

- (IBAction)buttonTouched:(id)sender
{
button.selected = !button.selected;
// add other logic
}

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
}

How to UI test a checkbox button

When the checkbox is selected, add the selected accessibility trait. Then, in your UI test, check the element's isSelected property.

// App code
imageView.accessibilityTraits = imageView.accessibilityTraits.union([.selected])

// Test code
XCTAssertTrue(checkboxElement.isSelected)

Remember to add logic to remove the selected trait when the checkbox is unselected.

How to set the value of checkboxes with a single variable

Your every defined button can hold the state of your checkbox.
You need to use normal and selected state of your button.

     private lazy var btnClick:UIButton = {
var btn = UIButton()
btn.setImage(UIImage(named: "ImgcheckBox"), for: .normal)
btn.setImage(UIImage(named: "imgCheckSelected"), for: .selected)
btn.addTarget(self, action: #selector(self. checkBtnpressed(btn:)), for: .touchUpInside)
return btn
}()

@objc func checkBtnpressed(btn: UIButton) {
btn.isSelected = !btn.isSelected
}

Now when you need all the selected checkboxes, Just iterate through your buttons and pick the values whose isSelected property is true.

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.

What is the best way to make a UIButton checkbox?

You shouldn't need to subclass the UIButton class. By design, Objective-C favors composition over inheritance.

UIButton is a subclass of UIControl, which has a selected property. You can use this property to toggle the on/off behaviour of a checkbox, just the same way a UISwitch does.

You can attach an action to the button's touched up inside event, and perform the toggling in there, something like this:

// when you setup your button, set an image for the selected and normal states
[myCheckBoxButton setImage:checkedImage forState:UIControlStateSelected];
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateNormal];

- (void)myCheckboxToggle:(id)sender
{
myCheckboxButton.selected = !myCheckboxButton.selected; // toggle the selected property, just a simple BOOL
}

Add a checkbox (or similar) to iPhone app using XCode

Checkboxes are for OS X. The iOS equivalents are switches. If they aren't appropriate, you can roll your own checkbox using buttons.



Related Topics



Leave a reply



Submit