iOS控件--UISwitch--开关控件

UISwitch 继承自 UIControl 两种操作选项 开/关

属性和方法识别

初始化方法(iOS 系统内置了UISwithch控件的size,所以通过代码调整UISwithch的大小无效.默认大小 51.0f 31.0f)

UISwitch *switch = [[UISwitch alloc] initWithFrame:CGRectMake(50,50, 100, 30)];

设置初始状态(默认状态为NO)

 switch.on = YES;

设置按钮处于关闭状态时边框的颜色

switch.tintColor = [UIColor blueColor];

设置开关处于开启时的状态

switch.onTintColor = [UIColor blackColor];

设置开关的状态钮颜色

switch.thumbTintColor = [UIColor greenColor];

在iOS 6及更早版本中,当开关处于打开 位置时显示图像。在iOS 7以后,设置无效

switch.onImage = [UIImage imageNamed:@"testImgOn"];

在iOS 6和更早版本中,当开关处于 关闭 位置时显示图像。在iOS 7以后,设置无效

switch.offImage = [UIImage imageNamed:@"testImgOff"];

整个开关背景色,设置后可以明显看到一个矩形背景

switch.backgroundColor = [UIColor redColor];

添加点击事件

[switch addTarget:self action:@selector(valueChangedMethod:) forControlEvents:(UIControlEventValueChanged)];
以上是对 UISwitch 控件的描述和使用,不以偏概全,只授之以渔,有更好的操作也会及时更新。如果您有UISwitch 控件的更好使用欢迎留言交流!

猜你喜欢

转载自blog.csdn.net/weixin_33819479/article/details/87502928