Objective
In the previous blog post, we introduced how to implement Uiswitch, and we introduced how to customize Uiswitch.
Original link: http://www.cnblogs.com/LeoYoung/p/qq907596253.html
Body
1, we first implement a uiswitch on the interface.
A> in the controller. m file, add a Uiswitch property.
1 @interface moboviewcontroller ()
2 @property (nonatomic, Strong) Uiswitch *Mainswtich;
3 @end
B> instantiates a uiswitch in the-(void) Viewdidappear method and adds it to the interface.
 
1 -(void ) Viewdidappear: (BOOL) animated 2 { 3 5 Self.mainswtich = [[Uiswitch alloc ]initwithframe:cgrectmake (100 , 100 , Span style= "color: #800080;" >0 , 0 )];//instantiation with x100,y100 Span style= "color: #008080;" > 6 7 [Self.view addSubview:self.mainSw Tich]; Add to Interface 8}
2. Custom Switch Color:
We first press CMD to enter the Uiswitch header file. See Three properties:
Uicolor *ontintcolor//color on Open
Uicolor *tintcolor//Off color
Uicolor *thumbtintcolor//Switch button color
Below we come to define, add the following code in-(void) Viewdidappear:
1Self.mainSwtich.thumbTintColor = [Uicolor colorwithred: (151./255.0) Green: (Bayi./255.0) Blue: (229./255.0) Alpha:1];2 3Self.mainSwtich.tintColor = [Uicolor colorwithred: (Wuyi./255.0) Green: (181./255.0) Blue: (229./255.0) Alpha:1];4 5Self.mainSwtich.onTintColor = [Uicolor colorwithred: (Wuyi./255.0) Green: (181./255.0) Blue: (229./255.0) Alpha:1];
Note: We have used the Uicolor class of ColorWithRed:green:blue: Method, the color value of this method is floating point type, so must use "color value./255.0", the color value is best with one behind. , look professional a little ~ ~ ~
In addition the color value conversion of God horse, there is a website support Online conversion: http://www.atool.org/colorpicker.php
3. We will set the default switch state of Uiswitch:
This is simple, add the following code in the-(void) Viewdidappear method:
1 [Self.mainswtich seton:yes animated:yes];
4, monitor the switch state of Uiswitch:
1 [Self.mainswtich addtarget:self Action: @selector (switchischanged:) forControlEvents: Uicontroleventvaluechanged];
Then, under-(void) Viewdidappear, implement the Switchischanged method:
1- (void) switchischanged: (Uiswitch *) Paramsender2 {3 if([Self.mainswtich isOn]) {4NSLog (@"Switch is on");5 }6 Else{7NSLog (@"Switch is off");8 }9}
Cmd+r run a bit, click on the switch, watch the console can see the results of the console printing!
Ios7_ about uiswitch_02_ How to customize Uiswitch_ how to set the default switch state of Uiswitch _ How to monitor switch switch status