iOS 小知识-设置UISwitch的颜色

效果图如下
iOS 小知识-设置UISwitch的颜色_第1张图片

    lazy var mySwitch:UIView = {
        let sw = UISwitch.init()
        sw.onTintColor = UIColor.red
        return sw;
    }()

    lazy var mySwitch2:UIView = {
        let sw = UISwitch.init()
        sw.tintColor = UIColor.lightGray//边缘
        sw.backgroundColor = UIColor.lightGray
        sw.layer.cornerRadius = sw.bounds.height/2.0
        sw.layer.masksToBounds = true
        return sw;
    }()

    lazy var mySwitch3:UIView = {
        let sw = UISwitch.init()
        sw.thumbTintColor = UIColor.purple
        return sw;
    }()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let stackView:UIStackView = UIStackView(frame: CGRect(x: 0, y: 60, width: 320, height: 450))
        stackView.axis = UILayoutConstraintAxis.vertical
        stackView.spacing = 20
        stackView.alignment = UIStackViewAlignment.center
        stackView.distribution = UIStackViewDistribution.equalSpacing
        self.view.addSubview(stackView)

        //改变选中背景色
        stackView.addArrangedSubview(mySwitch)

        //改变off颜色
        stackView.addArrangedSubview(mySwitch2)

        //改变按钮颜色
        stackView.addArrangedSubview(mySwitch3)

    }

你可能感兴趣的:(iOS)