我在这里想做的是,我希望用户在具有多个 UIButton 的 View Controller 中选择一个自定义 UIButton,但是如果用户选择一个 UIButton,我如何才能做到这一点,如果他想要选择另一个,用户点击的第一个 UIButton 将被取消选择。
如何让用户只选择一个 UIButton?如果一个 UIButton 被选中,另一个被选中,前者将被取消选中
最佳答案
所以你在按钮周围有某种边框,然后你可以遍历所有按钮并找到带有边框的按钮,将其删除,然后将边框设置为新选择的按钮。像这样:
func buttonSelected(clickedButton: UIButton) {
for case let button as UIButton in self.view.subviews {
if button.layer.borderColor == UIColor.black.cgColor {
// deselect it here by changing the border
button.layer.borderColor = UIColor.clear.cgColor
}
// select the clicked button
clickedButton.layer.borderColor = UIColor.black.cgColor
}
}
关于ios - 如何让用户在具有多个 UIButton 的 View 中只选择一个自定义 UIButton? swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43967662/