How to Create a Simple Checkbox in iOS

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!

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.

How can I create a text with Checkbox in SwiftUI?

We can take help of the @State from Apple, which persists value of a given type, through which a view reads and monitors the value.

Working example :

struct CheckboxFieldView: View {

@State var checkState: Bool = false

var body: some View {

Button(action:
{
//1. Save state
self.checkState = !self.checkState
print("State : \(self.checkState)")


}) {
HStack(alignment: .top, spacing: 10) {

//2. Will update according to state
Rectangle()
.fill(self.checkState ? Color.green : Color.red)
.frame(width:20, height:20, alignment: .center)
.cornerRadius(5)

Text("Todo item ")

}
}
.foregroundColor(Color.white)

}

}

Now, you can add CheckboxFieldView()

Make CheckBox in swift for IOS

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.

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
}

Create Checkbox using Storyboard in Xcode

CheckBox is not available in object library. You can use third party library for that purpose or you can create it by your self.

There is the working code for checkbox.

create a class level variable and property of button in @inteface

@interface testViewController (){
BOOL checkBoxSelected;
}
@property (weak, nonatomic) IBOutlet UIButton *checkBox;

@end

in viewdidload set images for the button states.

 [_checkBox setBackgroundImage:[UIImage imageNamed:@"checkBoxUnChecked.png"]
forState:UIControlStateNormal];
[_checkBox setBackgroundImage:[UIImage imageNamed:@"checkBoxChecked.png"]
forState:UIControlStateSelected];

and after create a button action in that button Action.

checkBoxSelected = !checkBoxSelected; /* Toggle */
[_checkBox setSelected:checkBoxSelected];

Hope it helps

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
}


Related Topics



Leave a reply



Submit