Change color of UISwitch in “off” state

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



    Working 100% IOS 13.0 and Swift 5.0 switch both state color set same #ios13 #swift #swift5

    @IBOutlet weak var switchProfile: UISwitch!{
        didSet{
            switchProfile.onTintColor = .red
            switchProfile.tintColor = .red
            switchProfile.subviews[0].subviews[0].backgroundColor = .red
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-11-30 21:37

    Swift 5:

    import UIKit
    
    extension UISwitch {    
    
        func set(offTint color: UIColor ) {
            let minSide = min(bounds.size.height, bounds.size.width)
            layer.cornerRadius = minSide / 2
            backgroundColor = color
            tintColor = color
        }
    }
    
    0 讨论(0)
  • 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:

    0 讨论(0)
  • 2020-11-30 21:39

    XCode 11, Swift 5

    I don't prefer using subViews, cause you never know when apple gonna change the hierarchy.

    so I use mask view instead.

    it works with iOS 12, iOS 13

        private lazy var settingSwitch: UISwitch = {
            let swt: UISwitch = UISwitch()
            // set border color when isOn is false
            swt.tintColor = .cloudyBlueTwo
            // set border color when isOn is true
            swt.onTintColor = .greenishTeal
    
            // set background color when isOn is false
            swt.backgroundColor = .cloudyBlueTwo
    
            // create a mask view to clip background over the size you expected.
            let maskView = UIView(frame: swt.frame)
            maskView.backgroundColor = .red
            maskView.layer.cornerRadius = swt.frame.height / 2
            maskView.clipsToBounds = true
            swt.mask = maskView
    
            // set the scale to your expectation, here is around height: 34, width: 21.
            let scale: CGFloat = 2 / 3
            swt.transform = CGAffineTransform(scaleX: scale, y: scale)
            swt.addTarget(self, action: #selector(switchOnChange(_:)), for: .valueChanged)
            return swt
        }()
    
        @objc
        func switchOnChange(_ sender: UISwitch) {
            if sender.isOn {
                // set background color when isOn is true
                sender.backgroundColor = .greenishTeal
            } else {
                // set background color when isOn is false
                sender.backgroundColor = .cloudyBlueTwo
            }
        }
    
    
    0 讨论(0)
提交回复
热议问题