Turn on / off UISwitch

I want to set my switch as follows:

enter image description here

But I try in ios9, this does not work. I saw in the Apple UISwitch Class Reference. It states that:

Discussion In iOS 7, this property has no effect.

What about iOS 9? Any success?

My code is:

switch1 = UISwitch(frame:CGRectMake(self.view.frame.width/2 - 20, 400, 10, 100)) switch1.on = true switch1.onTintColor = UIColor.lightGrayColor() switch1.tintColor = UIColor.greenColor() switch1.thumbTintColor = UIColor.blackColor() 

// set image on / off

 switch1.onImage = UIImage(named: "on-switch") switch1.offImage = UIImage(named: "off-switch") 
+6
source share
1 answer

Use UIButton .

 let switchButton = UIButton(type: .Custom) switchButton.selected = true switchButton.setImage(UIImage(named: "on-switch"), forState: .Selected) switchButton.setImage(UIImage(named: "off-switch"), forState: .Normal) 

Use switchButton.selected instead of switch1.on . You will need to switch switchButton.selected when it is used ( switchButton.selected = !switchButton.selected ).

+15
source

All Articles