UISwtch 在许多场景中会使用到,通常用来坐设置开关等行为。下面我们来看一下IOS 中 UISwitch 等用法。注意:UISwitch 无法通过 frame 设置宽高
UISwitch* _switch = [[UISwitch alloc] init];// 设置状态和开启动画[_switch setOn:NO animated:YES];// 设置开启状态颜色[_switch setOnTintColor: [MyUI getThemeColor]];// 设置位置_switch.frame = CGRectMake(100, 100, 0, 0);// 绑定事件[_switch addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];[self.view addSubview:_switch];
属性
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
onTintColor | UIColor | 风格颜色 | nil |
thumbTintColor | UIColor | 原点颜色 | whiteColor |
onImage | UIImage | 开启状态显示图片 | nil |
offImage | UIImage | 关闭状态显示图片 | nil |
style | UISwitchStyle | 开关的样式,可修改大小 | UISwitchStyle |
on | BOOL | 开关的状态 | NO |
API
-
- (instancetype)initWithFrame:(CGRect)frame
创建UISwitch同时设置位置 -
- (void)setOn:(BOOL)on animated:(BOOL)animated
设置状态并开启动画