xcode UISwitch 设置开/关图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36928861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:46:14  来源:igfitidea点击:

UISwitch set on/off Image

xcodeswiftuiswitch

提问by YungCheng Su

I want to set my Switch like this:

我想这样设置我的 Switch:

enter image description here

在此处输入图片说明

But I try in ios9 , it does not work. I saw in apple UISwitch Class Reference. It says that :

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

Discussion In iOS 7, this property has no effect.

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

How about iOS 9? Any one success?

iOS 9 怎么样?任何一个成功?

My Code:

我的代码:

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

//set on/off image

//设置开/关图像

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

回答by rob mayoff

Use a UIButtoninstead.

使用 aUIButton代替。

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

Use switchButton.isSelectedinstead of switch1.on. You'll have to toggle switchButton.isSelectedwhen it is tapped, which you can do like this:

使用switchButton.isSelected代替switch1.on。您必须switchButton.isSelected在点击它时进行切换,您可以这样做:

switchButton.isSelected.toggle()

回答by Kasey

Not an exact answer to your question, but if you want a completely custom button switch programmatically (that you can add text to), this will work too:

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

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