Change color of UISwitch in “off” state

前端 未结 18 2766
忘掉有多难
忘掉有多难 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条回答
  •  猫巷女王i
    2020-11-30 21:33

    More safe way in Swift 3 without magical 16pt values:

    class ColoredBackgroundSwitch: UISwitch {
    
      var offTintColor: UIColor {
        get {
          return backgroundColor ?? UIColor.clear
        }
        set {
          backgroundColor = newValue
        }
      }
    
      override func layoutSubviews() {
        super.layoutSubviews()
        let minSide = min(frame.size.height, frame.size.width)
        layer.cornerRadius = ceil(minSide / 2)
      }
    
    }
    

提交回复
热议问题