UI<06>

//声明对象
@property (nonatomic,strong) UISwitch *switchX;
 
    self.switchX = [[UISwitchalloc] init];
    self.switchX.frame = CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2,51, 31);
    //用于打开开关外观的颜色
    self.switchX.onTintColor = [UIColor greenColor];
    //用于在开关关闭时调整开关轮廓的颜色
    self.switchX.tintColor = [UIColor purpleColor];
    //thumb外观颜色
    self.switchX.thumbTintColor = [UIColor blueColor];
    //设置初始为ON
    [self.switchX  setOn:YES animated:YES];
   
    //添加事件
    [self.switchX addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.switchX];
 
 
//方法的实现
- (void)switchAction:(UISwitch *)click{
   
     NSLog(@"%@", click.isOn ? @"开..." : @"关...");
 
}
原文地址:https://www.cnblogs.com/iQingYang/p/6688916.html