【问题标题】:UISwitch set on/off ImageUISwitch 设置开/关图像
【发布时间】:2016-08-24 01:19:59
【问题描述】:

我想这样设置我的 Switch:

但我尝试在 ios9 中,它不起作用。 我在苹果 UISwitch 类参考中看到了。 它说:

讨论 在 iOS 7 中,此属性无效。

iOS 9 怎么样?有没有成功的?

我的代码:

switch1 = UISwitch(frame:CGRectMake(self.view.frame.width/2 - 20, 400, 10, 100))
switch1.on = true
switch1.onTintColor = UIColor.lightGrayColor()
switch1.tintColor = UIColor.greenColor()
switch1.thumbTintColor = UIColor.blackColor()

//设置开/关图像

switch1.onImage = UIImage(named: "on-switch")
switch1.offImage = UIImage(named: "off-switch")

【问题讨论】:

    标签: xcode swift uiswitch


    【解决方案1】:

    请改用UIButton

    let switchButton = UIButton(type: .Custom)
    switchButton.selected = true
    switchButton.setImage(UIImage(named: "on-switch"), forState: .Selected)
    switchButton.setImage(UIImage(named: "off-switch"), forState: .Normal)
    

    使用switchButton.isSelected 而不是switch1.on。您必须在点击 switchButton.isSelected 时切换它,您可以这样做:

    switchButton.isSelected.toggle()
    

    【讨论】:

    • 感谢您的建议!在 Auto Layout 中设置 UISwitch onImage 和 offImage 有点误导,没有任何警告。
    • 希望我早点碰到这个,而不是花很多时间尝试使用 3rd 方库或自己做啊,就像一个魅力!
    【解决方案2】:

    对于 iOS 13,您可以这样做:

       let switcher = UISwitch()
        switcher.addTarget(self, action: #selector(pressed), for: .valueChanged)
        
    
     @objc func pressed(sender: UISwitch) {
            let color = UIColor(patternImage: UIImage(named: sender.isOn ? "on.png": "off.png")!)
            if sender.isOn {
                sender.onTintColor = color
            } else {
                sender.tintColor = color
                sender.subviews[0].subviews[0].backgroundColor = color
            }
        }
    

    注意:您的图片应如下所示:

    那么最终的结果是:

    【讨论】:

      【解决方案3】:

      不是您问题的确切答案,但如果您想要以编程方式完全自定义按钮开关(您可以添加文本),这也可以:

       import UIKit
      
       class RDHiddenVisibleButton: UIButton {
      
          // Hidden / Visible Button Function
          var isOn = false
      
          override init(frame: CGRect) {
              super.init(frame: frame)
              initButton()
          }
      
          required init?(coder aDecoder: NSCoder) {
              super.init(coder: aDecoder)
              initButton()
          }
      
          func initButton() {
              layer.borderWidth = 2.0
              layer.borderColor = Colors.radiusGreen.cgColor
              layer.cornerRadius = frame.size.height/2
      
              setTitleColor(Colors.radiusGreen, for: .normal)
              addTarget(self, action: #selector(RDHiddenVisibleButton.buttonPressed), for: .touchUpInside)
      
          }
      
      @objc func buttonPressed() {
              activateButton(bool: !isOn)
          }
      
          func activateButton(bool: Bool) {
      
              isOn = bool
      
              let color = bool ? Colors.radiusGreen : .clear
              let title = bool ? "Hidden" : "Visible"
              let titleColor = bool ? . white : Colors.radiusGreen
      
              setTitle(title, for: .normal)
              setTitleColor(titleColor, for: .normal)
              backgroundColor = color
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-04-25
        • 2023-03-11
        • 1970-01-01
        • 2014-09-13
        • 1970-01-01
        • 2017-01-07
        • 1970-01-01
        • 2023-03-18
        相关资源
        最近更新 更多