1.创建
UISwitch *RRPSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(RRPWidth - 60, 10, 45, 20)];
2.设置属性
<1>开启时的颜色
RRPSwitch.onTintColor = [UIColor yellowColor];
<2>关闭时的颜色
RRPSwitch.tintColor = [UIColor brownColor];
<3>开启时的图片
RRPSwitch.onImage = [UIImage imageNamed:@"save"];
<4>关闭时的图片
RRPSwitch.offImage = [UIImage imageNamed:@"bookTicket"];
<5>圆形按钮颜色
RRPSwitch.thumbTintColor = [UIColor blackColor];
<6>设置开关状态和动画效果
[RRPSwitch setOn:NO animated:YES];
<7>添加开关方法
[RRPSwitch addTarget:self action:@selector(switchSlideYesOrNo:) forControlEvents:(UIControlEventValueChanged)];
<8>加到要放的控件上
[self.view addSubview:RRPSwitch];
开关的实现
- (void)switchSlideYesOrNo:(UISwitch *)sender {
if (sender.isOn) {
NSLog(@"打开了");
}else{
NSLog(@"关闭");
}
}