Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing UISwitch width and height

Tags:

ios

uiswitch

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?

like image 679
Bharat Raichur Avatar asked Aug 03 '14 12:08

Bharat Raichur


3 Answers

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];
like image 111
William George Avatar answered Oct 08 '22 00:10

William George


Swift 4

@IBOutlet weak var switchDemo: UISwitch!

override func viewDidLoad() {
    super.viewDidLoad()
   switchDemo.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
}
like image 37
Barath Avatar answered Oct 08 '22 00:10

Barath


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.

like image 17
Warren Burton Avatar answered Oct 07 '22 23:10

Warren Burton