我是swift的新手,我想用UIButton製作切換/切換功能。用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
}
什麼是'self.wbButtons' –
這是多個按鈕的出口集合。 – GilbertOOl
點擊按鈕後你想要什麼? –