Change color of UISwitch in “off” state

前端 未结 18 2798
忘掉有多难
忘掉有多难 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:36

    Here's a pretty good trick: you can just reach right into the UISwitch's subview that draws its "off" background, and change its background color. This works a lot better in iOS 13 than it does in iOS 12:

    if #available(iOS 13.0, *) {
        self.sw.subviews.first?.subviews.first?.backgroundColor = .green
    } else if #available(iOS 12.0, *) {
        self.sw.subviews.first?.subviews.first?.subviews.first?.backgroundColor = .green
    }
    

提交回复
热议问题