How to create Custom Generic CheckBox View in Swift like Android?

Like Android, In iOS, there is no native checkbox component to use as multiple-choice options. We will create a generic checkbox view to use multiple times on any multiple ViewControllers. This is how our custom checkbox will look like.

You can have source code at this link:

We will create one generic component so that we can use same checkbox view in multiple ViewControllers.

Let’s create new project named as GenericCheckBoxDemo.

Create project For create generic Check Box View Demo.

Let’s create a custom generic/custom view for the checkbox, which can be accessed from any ViewController through the project.

Name the File Name / Class Name to our generic check box view, I named it CheckboxView, You can change it as per your considerations.

Add XIB for CheckboxView view.

After creating custom class for CheckBoxView. we have to design custom view in it to show checkbox.

1 ) Take one parent View, and on parentView add one UIImageView to show checkbox Image.

2) Take UIButton to take tap event on checkBox.
Note: Leading, Trailing, Height, and width of UIButton should be the same as per UIImageView taken earlier.

3) Add Outlets of parent view and imageView and in CheckboxView.swift class

Add the following code to create CheckboxView as a generic component.
– Add CheckboxView property in CheckboxView.Swift class with the name of viewFromNib.

Created function commonSetUp() for initialize instance and add viewFromNib as a subview in the same class. Call this function from init()

To handle status of checkbox create enum.

Also add the instance of enum to handle the state of checkbox.

On tap of button we have to change the checkBoxState.

Here you will get an error of missing updateCheckBox() function. In this function, we are updating UI like checkImageView image and checkImageBGView layer border.

Before applying this function we want checkbox image named as “tick” in our bundle.

Finally you have created generic checkboxView like Android

Just need to create one view in your viewController and apply CheckboxView as custom class.

You can have source code at this link:

Leave a Comment