Simple Toggle Control in iOS
Published in
2 min readOct 12, 2019
like Android toggle switch
The toggle switch in iOS looks great, and also supports Haptic feed back, so you might not get a use case to customise it. We can add custom animations, and also can support images or text on thumb control, etc. But this tutorial covers very basic control(Android like toggle) setup.
This how it looks :
Let’s start :
- Create an IBDesignable class for the control.
IBDesignable
attribute helps to setup the UI Element through storyboards. - Define the IBInspectable properties for colors and toggle status, etc that we need to access or set the values.
IBInspectable
attributes help to configure the UI Element through storyboards & attribute inspector. - Here I used draw method, to render the control. We can use helper methods also if customisation requires.
- And finally on touches ended, I am sending the notification for
valueChanged.
by toggling theon
property on control. We can use the remaining touch event methods, to drag along with the touch, and for more customised state animations.
5. I provided the comments for the code, so you can understand what it does.