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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值