Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode UI开关设置开/关图像_Xcode_Swift_Uiswitch - Fatal编程技术网

Xcode UI开关设置开/关图像

Xcode UI开关设置开/关图像,xcode,swift,uiswitch,Xcode,Swift,Uiswitch,我想这样设置我的开关: switchButton.isSelected.toggle() 但我在ios9中尝试,它不起作用。 我在苹果UISwitch课堂上看到了参考资料。 它说: 讨论 在iOS 7中,此属性无效 iOS9怎么样?有人成功了吗 我的代码: switch1 = UISwitch(frame:CGRectMake(self.view.frame.width/2 - 20, 400, 10, 100)) switch1.on = true switch1.onTintColor

我想这样设置我的开关:

switchButton.isSelected.toggle()

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

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

iOS9怎么样?有人成功了吗

我的代码:

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")

改用
ui按钮

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
。点击开关按钮时,您必须切换开关按钮。isSelected,您可以这样做:

switchButton.isSelected.toggle()

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

 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
    }
}

对于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
        }
    }
注意:您的图像应如下所示:

最后的结果是:


谢谢你的建议!在没有任何警告的情况下,让UISwitch onImage和offImage在自动布局中是有点误导性的。但愿我能早点遇到这个问题,而不是花很多时间尝试使用第三方库或自己做啊哈,这真是一个魅力!