38

I am trying to change the default height and width of a UISwitch element in iOS, but unsuccessfully.

Can you change the default height and width of a UISwitch element?
Should the element be created programmatically?

Forge
  • 6,538
  • 6
  • 44
  • 64
Bharat Raichur
  • 425
  • 1
  • 4
  • 8

7 Answers7

49

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];
iHarshil
  • 739
  • 10
  • 22
William George
  • 6,735
  • 3
  • 31
  • 39
  • i want to make switch smaller in size . do you have any other way ? how can i do it ? – Moxarth Jul 17 '17 at 05:19
  • got it . your code helps me to solve the issue . thanks . – Moxarth Jul 17 '17 at 09:31
  • This method causes pixellation artifacts. The outside of the switch is rendered sharply (probably because it uses CALayer cornerRadius,) but the inside knob is a bitmap and pixellates when upscaled. – TyR Sep 04 '20 at 18:20
33

Swift 4

@IBOutlet weak var switchDemo: UISwitch!

override func viewDidLoad() {
    super.viewDidLoad()
   switchDemo.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
}
Barath
  • 1,656
  • 19
  • 19
21

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)
    }
}
Ivan
  • 1,320
  • 16
  • 26
18

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.

Warren Burton
  • 17,451
  • 3
  • 53
  • 73
  • 1
    I wonder why Apple restricts the width of a UISwitch? What's so wrong about extending the width? Seems odd but thanks for this anyway. – Supertecnoboff Oct 08 '19 at 16:40
15

here is a nice UISwitch subclass that i wrote for this purpose, its also IBDesignable so you can control it from your Storyboard / xib

@IBDesignable class BigSwitch: UISwitch {

    @IBInspectable var scale : CGFloat = 1{
        didSet{
            setup()
        }
    }

    //from storyboard
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    //from code
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    private func setup(){
        self.transform = CGAffineTransform(scaleX: scale, y: scale)
    }

    override func prepareForInterfaceBuilder() {
        setup()
        super.prepareForInterfaceBuilder()
    }


}
Benny Davidovitz
  • 1,152
  • 15
  • 18
  • For some reason this code does not update Switch in storyboard. Still valid for running app – dandepeched Mar 17 '19 at 17:58
  • I am not sure what is the reason the you can not see the switch in your storyboard, I checked it now and its working. Anyway Don't forget that in order to see the switch as bigger in the storyboard, you have to set the scale in the attributes inspector. – Benny Davidovitz Mar 17 '19 at 18:29
  • That's strange... I copied code above completely and of cause changed scale in inspector. Using Xcode 10.1 – dandepeched Mar 17 '19 at 19:09
  • try to write something else in the setup method like setting a value onTint color, and see if the storyboard displays the new color – Benny Davidovitz Mar 17 '19 at 19:21
  • I rebooted Xcode and switch starts scaling =) Thanks for your help! – dandepeched Mar 17 '19 at 19:27
2
import UIKit

extension UISwitch {

    static let standardHeight: CGFloat = 31
    static let standardWidth: CGFloat = 51
    
    @IBInspectable var width: CGFloat {
        set {
            set(width: newValue, height: height)
        }
        get {
            frame.width
        }
    }
    
    @IBInspectable var height: CGFloat {
        set {
            set(width: width, height: newValue)
        }
        get {
            frame.height
        }
    }
    
    func set(width: CGFloat, height: CGFloat) {

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

        transform = CGAffineTransform(scaleX: widthRatio, y: heightRatio)
    }
}
Luda
  • 7,282
  • 12
  • 79
  • 139
1

Even if it’s possible to make a UISwitch smaller, this would negatively effect the user experience. Apple's Human Interface Guidelines recommend a minimum size of 44 points for touch targets.

Provide ample touch targets for interactive elements. Try to maintain a minimum tappable area of 44pt x 44pt for all controls

By scaling this to smaller than the standard size, it will become more difficult for users to tap, and also introduce accessibility concerns. Please consider users with less than perfect vision or motor control before making UI elements small.

Finally, here’s an excerpt from a great article about touch target sizes illustrating what can happen when controls are too small.

Interviewer — “I noticed, you had some trouble submitting your email address on this screen, can you tell me how that felt?”

User — “Oh yeah, I’m not very good at technology.”

Interviewer — “What do you think was causing you to struggle at that point?”

User — “The buttons were hard to tap, and I just kept stuffing it up.”

Aaron T
  • 159
  • 1
  • 6
  • 1
    Maybe I am getting something wrong but if they recommend a default minimum size for tappable controls to be 44px x 44px - why is their default switch dimensions 51 x 31 – Shawn Frank Jul 16 '21 at 11:25
  • It’s true, Apple is violating their own guideline here. But this is all the more reason to not make it any smaller! – Aaron T Jul 19 '21 at 15:57
  • @AaronT who's trying make it smaller ? I don't see here anyone, but expect most people want it larger obviously.. – Renetik May 24 '22 at 06:08