Introduction to How to create check box in swift – tutorial
In iOS, there is no default uicontrol for creating check box. Thus we have to create our own check box in swift, to use in our iOS apps by using other uicontrol’s like UIButton. In this tutorial, we will learn how to create check box in swift in ios sdk and also animating it while changing checkbox state from un-selected to selected and vice versa.
Steps to create a checkbox in iOS using swift language
Step 1: Create a new xcode project using single view application template and named it “AnimatedCheckbox”.
Step 2: Open “Main.storyboard” and drag UIButton from object library to our ViewController.
Step 3: Add constraints to the UIButton as shown below
Step 4: Download the images for Check box from here AssetsCheckmark.zip
Step 5: Add the assets/images to your projects “Assets.xcassets”.
Step 6: Open “ViewController.swift”, and create an IBOutlet for the button which is going to be used as check box for our app. Also we will assign our image/assets to button, un-selected checkbox image is assigned to default/normal state of UIButton and selected checkbox image is assigned to selected state of UIButton. Lastly, we will create an IBAction for UIButton so that we can detect the touch event,
Step 7: Open “Main.storyboard” and connect IBOutlet and IBaction(touchupinside event) to button.
Step 8: Also change button type and remove title as shown in the image below
Step 9: Open “ViewController.swift” so that we can add code inside our IBAction “checkMarkTapped” , in order to make btnCheckMark behave like a Checkbox. Below is the code
Step 10: Run your app and tap on Button with green circle image, you will see checkbox on or off / selected or UN-selected.
Where to go from here:
In this tutorial, we learned how to create a checkbox in swift using UIButton in ios sdk, The tutorial is basic and simple so that one can understand how we can use check box in iOS app using swift language because i received few emails of blog follower who are no-voice to iOS app development. If you have any question then please feel free to comment them out. You can also download the sample code for the tutorial on creating checkbox in iOS using swift language from this link AnimatedCheckbox.zip