【发布时间】:2017-09-05 01:07:32
【问题描述】:
我是 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 方法中声明),我希望工作正常,但 ONE TIME 并且我一直希望这个工作。
if switchDarkBT.isSelected == false {
switchDarkBT.isHidden = false
switchLightBT.isHidden = true
} else {
switchDarkBT.isHidden = true
switchLightBT.isHidden = false
}
【问题讨论】:
-
什么是
self.wbButtons -
它是多个按钮的插座集合。
-
点击按钮后你想要什么?
-
点击按钮时要一个隐藏一个取消隐藏按钮吗?
-
嗨,我想隐藏/禁用“switchLightBT”并显示“switchDarkBT”,反之亦然。
标签: ios swift3 uibutton swift2 toggle