Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It turns out that you can create a custom UISwitch with the following items:</p> <ul> <li>A UIScrollView</li> <li>A UIButton</li> <li>Two UILabels</li> <li>A background image</li> <li>A Boolean value</li> </ul> <p>First you will need to add QuartzCore.framework to your project and <code>#import &lt;QuartzCore/QuartzCore.h&gt;</code> to your view controller.</p> <p>Next add the UIScrollView to your view using Interface Builder. The ScrollView will be your custom UISwitch. Next add the button and the two labels to your ScrollView. One label will be for "yes" the other for "no".</p> <p>Add the image to the button and set its type to custom. This is the image I use: <img src="https://i.stack.imgur.com/KjUcu.png" alt="enter image description here"></p> <p>Position the labels over the blue and white area of the image. Adjust the ScrollView so it is just big enough to show the blue part of the image and the thumb nob. </p> <p>Add the following line to viewDidLoad:</p> <pre><code>self.mySwitch.layer.cornerRadius = 13.5; </code></pre> <p>mySwitch is the name of the ScrollView and 13.5 is half the height of the ScrollView. The above statement changes the ScrollView to have rounded ends like the UISwitch.</p> <p>To make the custom switch active you will need to tie the buttons "Touch Up Inside" event to an IBAction. Here is the code I use in the event handler:</p> <pre><code>-(IBAction)mySwitchButton:(id)sender { self.myValue = !self.myValue; CGPoint scrollPoint = CGPointMake((self.myValue)? 43.0: 0, 0.0); [mySwitch setContentOffset:scrollPoint animated:YES]; } </code></pre> <p>Where myValue is the boolean variable that contains the state of your switch and 43.0 is the number of points you will have to move the image over to put the switch in the off position. </p> <p>That is all there is to it!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload