Change color of UISwitch in “off” state

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

    all I finally used transform and layer.cornerRadius too. But I have added translation to it to be center.

        private func setSwitchSize() {
        let iosSwitchSize = switchBlockAction.bounds.size
        let requiredSwitchSize = ...
        let transform = CGAffineTransform(a: requiredSwitchSize.width / iosSwitchSize.width, b: 0,
                                          c: 0, d:  requiredSwitchSize.height / iosSwitchSize.height,
                                          tx: (requiredSwitchSize.width - iosSwitchSize.width) / 2.0,
                                          ty: (requiredSwitchSize.height - iosSwitchSize.height) / 2.0)
    
        switchBlockAction.layer.cornerRadius = iosSwitchSize.height / 2.0
        switchBlockAction.transform = transform
    }
    

    And I did use backgroundColor and tintColor in designer. Hope it helps.

提交回复
热议问题