Creating programmatically a switch (UISwitch)


UISwitch is one of the best framework of UIKit. To create it programmatically, code:

UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(50, 100, 0, 0)];

Above range allocates an UISwitch and models its shape. UISwitch has resolved measurements (about 95 px extensive, 27px high), so you have to set the harmonizes for top-left part. Size and size are ignored, you can not create swap lesser or larger by modifying the shape.

To add it to your perspective you should use this code:

[self.view addSubview:mySwitch];

Be standard swap is set to Off. You are able to modify it’s condition programmaticaly using setOn method:

[mySwitch setOn:YES animated:YES];

To swap it rear again to Off use:

[mySwitch setOn:NO animated:YES];

In both above situations UISwitch changes it condition with movement (animated:YES), use animated:NO to modify condition without movement.

To have a look at UISwitch present condition use on residence, example:

if (mySwitch.on) NSLog(@"switch state: on");
else NSLog(@"switch state: off");

To avoid modifying the UISwitch condition set the empowered residence to NO:

mySwitch.enabled = NO; // or YES to reenable it

As I said before UISwitch is very easy framework – any measures – holding it causes UISwitch to modify it’s condition, so to telephone any strategy by modifying the UISwitch you have to add a focus on for ValueChanged event:

[mySwitch addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventValueChanged];

or

[mySwitch addTarget:self action:@selector(myMethod:) forControlEvents:UIControlEventValueChanged];

If you need a id to mySwitch item.

4 comments:

Nice work with explanation.I did something like this
http://maniishankar.wordpress.com/2013/05/21/iphone-uiswitch-example/

Nice Source for us, thanks for sharing this details and i read the whole information and it is really helpful for me.

Mobile App Development Company

Android App Development

If you are looking at the best android app development company, then IBC App Maker is the best android app development company that creating the android apps online at affordable price.

to get more - https://www.ibcappmaker.com/en/platforms/create-android-app-online

UISwitch is a view that is lets the user select a YES or NO much like a checkbox. You can set the initial state of the Switch and with the help of the target action for a change event.
Thanks for the interesting blog.

Vicky from Way2Smile Solutions DMCC - Digital Transformation Company in Dubai.

Post a Comment

Powered by Blogger.
Twitter Delicious Facebook Digg Stumbleupon Favorites More