1

I was trying to increase thumb size of a UISwitch I was trying to increase thumb size by set on the image but the image is not showing.

I try this:

uiSwitch.onImage = UIImage(named: "sing") 

i want switch like this

here is image

3 Answers3

3
if let thumbView = try (mySwitch.subviews[0].subviews[3] as? UIImageView) {
       thumbView.transform = CGAffineTransform(scaleX:1.5, y: 1.5)
   }
Mjd Mz
  • 121
  • 1
  • 5
  • 1
    Please add an explanation to your answer. – Flovdis Jun 08 '18 at 16:29
  • self explanatory code (thumbView = ...) I happend to have inspected the internals of UISwitch and found that the 4th subview (index 3) is the thumbView. Like any another view, you can increase/decrease it size by using the scale transform. Just incase it wasn't't obvious to some, if apple decides to change the structure of a UISwitch then the code won't work, (but it won't crash either because of the (as?) – Mjd Mz Jul 24 '18 at 13:17
2

Create extension :

extension UISwitch {

func increaseThumb(){
    if let thumb = self.subviews[0].subviews[1].subviews[2] as? UIImageView {
        thumb.transform = CGAffineTransform(scaleX:1.5, y: 1.5)
    }
  }
}

Use:

 customSwitch.increaseThumb(
Deep
  • 416
  • 6
  • 15
1

If you want exactly as image you refered https://github.com/JunichiT/JTMaterialSwitch

If you want to increase the only the thumb size of the UISwitch itself, you can't. You must increase the size of UISwitch.

snjmhj
  • 1,001
  • 11
  • 21