Member-only story

UISwitch iOS

Mr.Javed Multani
1 min readOct 5, 2020

--

Set Image for On/Off state

Objective-C

//set off-imagemySwitch.offImage = [UIImage imageNamed:@"off_image"];
[mySwitch setOffImage:[UIImage imageNamed:@"off_image"]];//set on-imagemySwitch.onImage = [UIImage imageNamed:@"on_image"];
[mySwitch setOnImage:[UIImage imageNamed:@"on_image"]];

Swift

//set off-imagemySwitch.offImage = UIImage(named: "off_image")//set on-imagemySwitch.onImage = UIImage(named: "on_image")

Set On / Off

Objective-C

[mySwitch setOn:YES];
//or
[mySwitch setOn:YES animated:YES];

Swift

mySwitch.setOn(false)
//or
mySwitch.setOn(false, animated: false)

Set Background Color

Objective-C

mySwitch.backgroundColor = [UIColor yellowColor];
[mySwitch setBackgroundColor: [UIColor yellowColor]];
mySwitch.backgroundColor =[UIColor colorWithRed:255/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
mySwitch.backgroundColor= [UIColor colorWithWhite: 0.5 alpha: 1.0];
mySwitch.backgroundColor=[UIColor colorWithHue: 0.4 saturation: 0.3 brightness:0.7 alpha: 1.0];

Swift

mySwitch.backgroundColor = UIColor.yellow
mySwitch.backgroundColor = UIColor(red: 255.0/255, green: 0.0/255, blue: 0.0/255, alpha: 1.0)
mySwitch.backgroundColor = UIColor(white: 0.5, alpha: 1.0)
mySwitch.backgroundColor = UIColor(hue: 0.4,saturation: 0.3,brightness: 0.7,alpha: 1.0)

Set Tint Color

Objective-C

//for off-statemySwitch.tintColor = [UIColor blueColor];
[mySwitch setTintColor: [UIColor blueColor]];//for on-statemySwitch.onTintColor = [UIColor cyanColor];
[mySwitch setOnTintColor: [UIColor cyanColor]];

Swift

//for off-statemySwitch.tintColor = UIColor.blueColor()//for on-statemySwitch.onTintColor = UIColor.cyanColor()

--

--

Mr.Javed Multani
Mr.Javed Multani

Written by Mr.Javed Multani

Software Engineer | Certified ScrumMaster® (CSM) | UX Researcher | Youtuber | Tech Writer

No responses yet