Custom UISwitch to show YES / NO

You can see this in action in my free water tracking application "Hydrate Yourself". If this helped you please consider downloading it and leaving a review.

One of the great things about iPhone development are that there are lots of user interface pieces that are easy to put together and customize. Unfortunately there are also a lot of pieces which don't have all the basic customizations that may people want. The UISwitch is one of those.

Here is the standard UISwitch:

Its size, colors, and ON / OFF text are not changeable. Fortunately there are several great re-implementations (and a few hacks that use restricted API calls) to modify all this. One that I am using is the RCSwitch class available here.

I made two small changes to this code, first I renamed his RCSwitchOnOff class to RCSwitchYesNo and changed the labels to YES / NO. Second I added an init method to the RCSwitch.m base class which called the usual init function but provided the default frame size. This works fine since I am positioning the x and y coordinates using the "center" property:


The final result looks and behaves just like the original UISwitch, but is actually completely rewritten using the UIControl class. My switches now look like this:

Comments

  1. Thanks for the lists. There are a lot of these lists on different peoples blogs these days, most of they are just way out of date though – I look forward to delving deeper into this list and hopefully contributing to some quality blogs.

    ReplyDelete

Post a Comment

Popular posts from this blog

Decommissioning the cloud: A self-hosted future

Using a JFileChooser to browse AWS S3

Taking Stuff Apart: Realistic Modulette-8