1.初始化UISwitch到界面
UISwitch* newsSwitch = [[UISwitch alloc]init];
cell.accessoryView= newsSwitch;
2.默認(rèn)開(kāi)啟
[newsSwitch setOn:YES animated:YES];
3.自定義UI
// 開(kāi)關(guān)開(kāi)啟時(shí)的顏色(默認(rèn)綠色)
newsSwitch.onTintColor= [UIColor redColor];
// 開(kāi)關(guān)圓圈的顏色(默認(rèn)白色)
newsSwitch.thumbTintColor= [UIColor redColor];
// 開(kāi)關(guān)關(guān)閉時(shí)的邊框顏色(默認(rèn)白色)
newsSwitch.tintColor= [UIColor redColor];
4.添加開(kāi)關(guān)的監(jiān)聽(tīng)事件
[newsSwitch addTarget:self action:@selector(switchChange:)forControlEvents:UIControlEventValueChanged];
- (void) switchChange:(UISwitch*)sender {
if([sender isOn]){
NSLog(@"turn on");
}else{
NSLog(@"turned off");
}
}