Search code examples
iosobjective-cuiviewios7uiswitch

How do I make a UISwitch under iOS 7 not take the background colour of the view behind it?


It looks like this whenever off:

enter image description here

While I'd prefer more of a grey background. Do I really have to use a UIImageView?


Solution

  • Here is how I changed the fill color of my iOS7 UISwitch.

    First you need to import QuartzCore.

    #import <QuartzCore/QuartzCore.h>
    

    Then set the background color and round the UISwitch's corners.

    UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 51.0, 31.0)];
    mySwitch.backgroundColor = [UIColor redColor];
    mySwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this.
    [self addSubview:mySwitch];
    

    This will give you a UISwitch with a custom off (background) color.

    Hope this helps someone:)