Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 321
Doug Smith Avatar asked Oct 04 '13 18:10

Doug Smith


2 Answers

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:)

like image 83
Barry Wyckoff Avatar answered Nov 16 '22 23:11

Barry Wyckoff


You can set the setOnTintColor property of your UISwitch to the color you desire.

like image 20
Antonio R. Avatar answered Nov 17 '22 00:11

Antonio R.