UISwitch

UISwitch继承于UIControl,通常被叫做开 关
 
初始化:
 
- (instancetype)initWithFrame:
//这个frame是没有意义的,系统的开关控件⼤⼩是确定的。(系统默认值)
 
onTintColor //设置开关开启状态时的颜⾊
tintColor //设置开关风格颜⾊
thumbTintColor //设置开关按钮颜⾊
 
onImage //设置开关开启状态时的图⽚(注意:在IOS7后不再起任何作⽤)
offImage //设置开关关闭状态时的图⽚(注意:在IOS7后不再起任何作⽤)
on //开关的状态
setOn:animated: //⼿动设置开关状态
 
//创建一个UISwitch 这里的frame只有origin起作用,size使用系统默认大小
    UISwitch *firstSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];
   
    firstSwitch.center = CGPointMake(self.view.center.x, 110);
   
    //设置开关开启状态时的颜色
    firstSwitch.onTintColor = [UIColor lightGrayColor];
   
    //设置开关风格颜色
    firstSwitch.tintColor = [UIColor blackColor];
   
    //设置开关按钮颜色
    firstSwitch.thumbTintColor = [UIColor orangeColor];
   
    //设置开关开启状态时的图片
    firstSwitch.onImage = [UIImage imageNamed:@"mm.JPG"];
   
    //开关的状态
    firstSwitch.on = NO;
 
    //手动设置开关状态
//    [firstSwitch setOn:YES animated:NO];
   
    [firstSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
   
    [self.view addSubview:firstSwitch];
    [firstSwitch release];
 
 
- (void)switchAction:(UISwitch *)mySwitch {
    if (mySwitch.on == YES) {
        NSLog(@"开启");
    } else {
        NSLog(@"关闭");
    }
}
原文地址:https://www.cnblogs.com/Walking-Jin/p/5210857.html