在swift中以编程方式使用UIButton切换函数

时间:2017-04-10 05:26:23

标签: ios swift3 uibutton swift2 toggle

我是swift的新手,我想用UIButton进行切换/切换功能。

我尝试了多种方法,但我找不到解决方案。

请你帮帮我。

这是我的代码(这些按钮在viewDidLoad方法中声明):

let switchLightBT = UIButton()
switchLightBT.frame = (frame: CGRect(x: 1, y: 220, width: 102, height: 102))
switchLightBT.backgroundColor = .clear
switchLightBT.tag = 1
switchLightBT.setImage(#imageLiteral(resourceName: "dark-icon"), for: .normal)
switchLightBT.addTarget(self, action: #selector(darkTheme(sender:)), for: .touchUpInside)
self.view.addSubview(switchLightBT)

let switchDarkBT = UIButton()
switchDarkBT.frame = (frame: CGRect(x: 1, y: 220, width: 102, height: 102))
switchDarkBT.backgroundColor = .clear
switchDarkBT.tag = 2
switchDarkBT.setImage(#imageLiteral(resourceName: "light-icon"), for: .normal)
switchDarkBT.addTarget(self, action: #selector(lightTheme(sender:)), for: .touchUpInside)

self.view.addSubview(switchDarkBT)

和行动:

    func darkTheme(sender: UIButton!){
        self.view.backgroundColor = .black
sender.isHidden = true
        for button in self.wbButtons {
            button.setTitleColor(UIColor .white, for: .normal)
        }
    }

    func lightTheme(sender: UIButton!){
        self.view.backgroundColor = .white
sender.isHidden = true
        for button in self.wbButtons {
            button.setTitleColor(UIColor .black, for: .normal)
        }
    }

只是为了预览,没有这个代码(也在viewDidLoad方法中声明),我希望工作正常但是一次,我一直希望这项工作。

if switchDarkBT.isSelected == false {
    switchDarkBT.isHidden = false
    switchLightBT.isHidden = true
} else {
    switchDarkBT.isHidden = true
    switchLightBT.isHidden = false

}

2 个答案:

答案 0 :(得分:1)

您可以将switchLightBT和switchDarkBT声明为实例变量,并将这些行添加到按钮函数中。

func darkTheme(sender: UIButton!){
        switchDarkBT.isHidden = true
        switchLightBT.isHidden = false
        self.view.backgroundColor = .black
        for button in self.wbButtons {
            button.setTitleColor(UIColor .white, for: .normal)
        }
    }

func lightTheme(sender: UIButton!){
        switchLightBT.isHidden = true
        switchDarkBT.isHidden = false
        self.view.backgroundColor = .white
        for button in self.wbButtons {
            button.setTitleColor(UIColor .black, for: .normal)
        }
        }

答案 1 :(得分:1)

试试这个

class ViewController: UIViewController { 
let switchLightBT = UIButton() 
let switchDarkBT = UIButton() 

override func viewDidLoad() { 
super.viewDidLoad() 
switchLightBT.frame = (frame: CGRect(x: 1, y: 220, width: 102, height: 102)) 
switchLightBT.backgroundColor = .clear 
switchLightBT.tag = 1 
switchLightBT.setImage(UIImage(named:"light"), for: .normal) 
switchLightBT.addTarget(self, action: #selector(darkTheme(sender:)), for: .touchUpInside) 
self.view.addSubview(switchLightBT) 

switchDarkBT.frame = (frame: CGRect(x: 1, y: 220, width: 102, height: 102)) 
switchDarkBT.backgroundColor = .clear 
switchDarkBT.tag = 2 
switchLightBT.setImage(UIImage(named:"dark"), for: .normal) 
switchDarkBT.addTarget(self, action: #selector(lightTheme(sender:)), for: .touchUpInside) 

self.view.addSubview(switchDarkBT) 

} 

func darkTheme(sender: UIButton!){ 
self.view.backgroundColor = .black 
switchLightBT.isHidden = true 
switchDarkBT.isHidden = false 

} 

func lightTheme(sender: UIButton!){ 
self.view.backgroundColor = .white 
switchLightBT.isHidden = false 
switchDarkBT.isHidden = true 

}