Simple Toggle Control in iOS

Leela Krishna
Oct 12, 2019 · 2 min read

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 :

Left is iOS & right is Android

Let’s start :

  1. Create an IBDesignable class for the control. IBDesignable attribute helps to setup the UI Element through storyboards.
  2. 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.
  3. Here I used draw method, to render the control. We can use helper methods also if customisation requires.
  4. And finally on touches ended, I am sending the notification for valueChanged. by toggling the on 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.


— — — — — — — — — *********************** — — — — — — — — —

You can contact / follow me on twitter & linkedIn accounts.

Thanks for reading…

***************************!!!See you!!!****************************

Swift India

Swift Developer Community in India 🇮🇳

Leela Krishna

Written by

A day time coder and a night time dreamer…

Swift India

Swift Developer Community in India 🇮🇳

More From Medium

More from Swift India

More from Swift India

Functional Swift: Closures { }

1.5K

More from Swift India

More from Swift India

More from Swift India

Learn about Struct in Swift

743

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade