others-how to create checkbox simply in an iOS app

1. Purpose

In this post, I would demo how to create a simple checkbox when developing an iOS app. This is the final result:

ezgif.com-gif-maker

2. Environment

  • Mac OS 10.15
  • Swift 5
  • Xcode 12

3. The solution

3.1 Add SimpleCheckBox to your pod dependency

SimpleCheckbox aims to accomplish what other ios checkbox controls haven’t. To be simple. There’s no animations, no IBDesignable to slow down interface builder, and no performance heavy draw methods.

image-20210428083924957

Edit your Podfile in your project, then add this:

target 'TestApp' do
  use_frameworks!

  pod 'SimpleCheckbox'
end

The key line is :

pod 'SimpleCheckbox'

Then install the dependency:

$ pod install --repo-update

Now you are ready to go.

3.2 Add import to your viewcontroller

Open your ViewController in the xcode editor, then input this:

import SimpleCheckbox

click Command+B to build the project again, if everything is fine, go on.

3.3 Add the view in story board

Add a UIView in story board to your view controller:

image-20210427201434447

And put a label beside it, just as follows:

image-20210427180324464

And make sure to change its custom class to Checkbox. Just as the right of the above picture shows.

3.3 Catch the click action of the checkbox

Then we can ctrl+dragging from the checkbox to our controller, make an IBAction of it, just as follows:

    @IBAction func onClickCheckbox(_ sender: Checkbox) {
        print("checked \(sender.isChecked)")
    }

Run the app, we get this when we check and uncheck the checkbox:

image-20210427170341400

Now it works!

4. Summary

In this post, I demonstrated how to make the button inside a tablecell clickable, make sure you have disabled the selection of the cell by choosing the ‘none’ selection type.

-->