UISwitch
是用于处理开关逻辑的控件。
UISwitch
不能指定大小,由系统默认指定,修改frame
不能更改它的大小。
指定150x50和200x75没有区别
可以依靠CGAffineTransformMakeScale(1.2, 1.2)
修改控件大小
主要属性
// 是否开启,默认是关闭
@property(nonatomic,getter=isOn) BOOL on;
// 控件开启状态的填充色
@property(nullable, nonatomic, strong) UIColor *onTintColor;
// 控件开关按钮的颜色
@property(nullable, nonatomic, strong) UIColor *thumbTintColor;
设置UISwitch
背景
- (UISwitch *)makeCustomSwitch {
UISwitch *customSwitch = [[UISwitch alloc] init];
customSwitch.onTintColor = [UIColor magentaColor];
customSwitch.thumbTintColor = [UIColor blueColor];
customSwitch.backgroundColor = [UIColor orangeColor];
customSwitch.layer.cornerRadius = 15;
customSwitch.layer.masksToBounds = YES;
return customSwitch;
}
显示如下
监听开关状态
UISwitch
添加状态监听器
[switch addTarget:self action:@selector(onSwitchValueChange:)
forControlEvents:UIControlEventValueChanged];
onSwitchValueChange:
方法监听修改
- (void)onSwitchValueChange:(UISwitch *)sender {
}