Changing UISwitch width and height

Swift 5:

import UIKit

extension UISwitch {

    func set(width: CGFloat, height: CGFloat) {

        let standardHeight: CGFloat = 31
        let standardWidth: CGFloat = 51

        let heightRatio = height / standardHeight
        let widthRatio = width / standardWidth

        transform = CGAffineTransform(scaleX: widthRatio, y: heightRatio)
    }
}

I tested the theory and it appears that you can use a scale transform to increase the size of the UISwitch

UISwitch *aSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(120, 120, 51, 31)];
aSwitch.transform = CGAffineTransformMakeScale(2.0, 2.0);
[self.view addSubview:aSwitch];

Swift 4

@IBOutlet weak var switchDemo: UISwitch!

override func viewDidLoad() {
    super.viewDidLoad()
   switchDemo.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
}

Not possible. A UISwitch has a locked intrinsic height of 51 x 31 .

You can force constraints on the switch at design time in the xib...

enter image description here

but come runtime it will snap back to its intrinsic size.

You can supply another image via the .onImage / .offImage properties but again from the docs.

The size of this image must be less than or equal to 77 points wide and 27 points tall. If you specify larger images, the edges may be clipped.

You are going to have to bake your own custom one if you want another size.

Tags:

Ios

Uiswitch