UI第十节——UISwitch

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 实例化UISwitch,固定大小
    UISwitch *swc = [[UISwitch alloc] initWithFrame:CGRectMake(40, 100, 295, 30)];
    
    // 主题颜色
    swc.thumbTintColor = [UIColor redColor];
    swc.onTintColor = [UIColor orangeColor];
    swc.tintColor = [UIColor greenColor];
    
    // 设置开启状态
    swc.on = YES;
    
    // 添加事件
    [swc addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    // 把UISwitch放到self.view上
    [self.view addSubview:swc];
}

- (void)switchValueChanged:(UISwitch *)swc
{
    // swc.on是开启还是关闭状态
    NSLog(@"%d", swc.on);
}

如果对你有帮助,请关注我哦!

原文地址:https://www.cnblogs.com/laolitou-ping/p/6244166.html