1.创建
self.switch1=[[UISwitch alloc] initWithFrame:CGRectMake(100, 200, 150, 50)];
[self.view addSubview:self.switch1];
[self.switch1 release];
2.颜色
self.switch1.onTintColor=[UIColor cyanColor];
self.switch1.tintColor=[UIColor redColor];
[self.switch1 setOn:YES animated:YES];
3.实现click方法
[self.switch1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
self.isOn=YES;
self.switch1.thumbTintColor=[UIColor blueColor];
4.click方法
-(void)click:(id)sender
{
if (self.switch1.isOn) {
NSLog(@"开关开启");
}else
{
NSLog(@"开关关闭");
}
}