Ui: uiswitch creation and use switch customization Switch

Source: Internet
Author: User

1. The uiswitch class provides an on/off user control.

- (void)viewDidLoad {    [super viewDidLoad];        self.mySwitch = [[UISwitch alloc]initWithFrame:CGRectMake(100, 100, 0, 0)];    [self.view addSubview:self.mySwitch];}
The default status of the switch control is disabled. You can change the on Attribute of the uisiwtch instance. You can also call the Seton method as follows:
[self.mySwitch setOn:YES];
If you want to receive a notification when the switch control is enabled or disabled, you must use the addtarget: Action: forcontrolevents: Method of uiswitch in your class to add the target of the switch.
[self.mySwitch addTarget:self action:@selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];- (void)switchIsChanged:(UISwitch *)paramSender{    NSLog(@"Sender is = %@",paramSender);    if ([paramSender isOn]) {        NSLog(@"The switch is turned on");    }else{        NSLog(@"The switch is turned off");    }}

2.

There are two main methods to customize the switch: tint colors: it can be applied to the UI component, such as uiswitch. The tint color is applied to the top position of the current color of the UI component. For example, you can set different colors for a common uiswitch. When the tint color is applied to the UI component, the original color of the control is minimized by the tint color. Images: Switch has two pictures: On Image: This picture indicates the switch is on. the width is 77 points, and the height is 22. off image: this image indicates the off status of the switch. the width is 77 points, and the height is 22. (the width and height are not necessarily the same)

2.1 modify the tint color of the switch. You can use the three important attributes of the uiswitch class to complete the following tasks:

Tincolor: This tint color will be used in the off state of the switch. Unfortunately, Apple did not replace tintcolor with offtintcolor. The type of this attribute is uicolor.

Thumbtintcolor:This tint color is used on the small circle button of the switch. This attribute is uicolor.

Ontintcolor:This tint color is used in the on state of the switch. This attribute is also uicolor.

Example:

// Set the on-mode tint color of the switch to red, the off-mode tint color to brown, and the tint color of the small circle button to green _ myswitch. tintcolor = [uicolor redcolor]; _ myswitch. ontintcolor = [uicolor greencolor]; _ myswitch. thumbtintcolor = [uicolor browncolor];
// Switch picture _ myswitch. onimage = [uiimage imagenamed: @ "on"]; _ myswitch. offimage = [uiimage imagenamed: @ "off"];

 

 

Ui: uiswitch creation and use switch customization Switch

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.