Change color of UISwitch in “off” state

前端 未结 18 2760
忘掉有多难
忘掉有多难 2020-11-30 20:53

I\'ve learned that we can change the UISwitch button appearance in its \"on\" state, but is it also possible to change the color of the UISwitch in the \"off\" state?

18条回答
  •  佛祖请我去吃肉
    2020-11-30 21:39

    My solution with #swift2:

    let onColor  = _your_on_state_color
    let offColor = _your_off_state_color
    
    let mSwitch = UISwitch(frame: CGRect.zero)
    mSwitch.on = true
    
    /*For on state*/
    mSwitch.onTintColor = onColor
    
    /*For off state*/
    mSwitch.tintColor = offColor
    mSwitch.layer.cornerRadius = mSwitch.frame.height / 2.0
    mSwitch.backgroundColor = offColor
    mSwitch.clipsToBounds = true
    

    Result:

提交回复
热议问题