iOS UI开发之UISwitch swift版本

作者: littleGG 发布时间: 2019-08-20 浏览: 1951 次 编辑

概述

  • UISwitch提供二进制选项的控件,如开/关。

  • UISwitch继承自UIControl

属性和方法

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

let mSwitch = UISwitch(frame: CGRect(x: 150, y: 250, width: 0, height: 0))


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

mSwitch.isOn = true


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

mSwitch.tintColor = UIColor.orange


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

mSwitch.onTintColor = UIColor.red


设置开关的状态钮颜色

mSwitch.thumbTintColor = UIColor.gray


开关处于打开位置时显示图像。

mSwitch.onImage = UIImage.init(named: "logo")


开关处于关闭位置时显示图像

mSwitch.offImage = UIImage.init(named: "logo2")


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

mSwitch.backgroundColor = UIColor.green


添加点击事件

mSwitch.addTarget(self, action: #selector(changed(mSwitch:)), for: UIControlEvents.valueChanged)