[英]Question about changing height of UIView when button is pressed in Swift
[英]Question about disabling one button when one button is active in Swift
上图有两个按钮。 按下左键后,背景被填充为红色。
如果我在这里按右键,我希望右键的背景填充为红色,左键为白色作为右键,并且该按钮被停用。
override func viewDidLoad() {
super.viewDidLoad()
bookTitleFilterBtn.addTarget(self, action: #selector(bookTitleFilterBtnClicked(_:)), for: .touchUpInside)
authorNameFilterBtn.addTarget(self, action: #selector(authorNameFilterBtnClicked(_:)), for: .touchUpInside)
}
//left button
@objc func bookTitleFilterBtnClicked(_ sender: UIButton) {
DispatchQueue.main.async {
if self.isHighlighted == false {
sender.backgroundColor = .red
let title = NSAttributedString(string: "제목", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
sender.setAttributedTitle(title, for: .normal)
sender.isHighlighted = true
self.isHighlighted = true
} else {
sender.backgroundColor = .white
let title = NSAttributedString(string: "제목", attributes: [NSAttributedString.Key.foregroundColor: UIColor.black])
sender.setAttributedTitle(title, for: .normal)
sender.isHighlighted = false
self.isHighlighted = false
}
}
}
//right button
@objc func authorNameFilterBtnClicked(_ sender: UIButton) {
DispatchQueue.main.async {
if self.isHighlighted == false {
sender.isHighlighted = true
let title = NSAttributedString(string: "작가", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
sender.setAttributedTitle(title, for: .normal)
sender.backgroundColor = .red
self.isHighlighted = true
} else {
sender.isHighlighted = false
self.isHighlighted = false
let title = NSAttributedString(string: "작가", attributes: [NSAttributedString.Key.foregroundColor: UIColor.black])
sender.setAttributedTitle(title, for: .normal)
sender.backgroundColor = .white
}
}
}
您忘记在第一种方法的第一个条件中更改backgroundColor
颜色。 为了防止更多此类问题,请尝试在 function 中定义逻辑并在需要的任何地方调用它,而不是一遍又一遍地重写它:
override func viewDidLoad() {
super.viewDidLoad()
bookTitleFilterBtn.addTarget(self, action: #selector(buttonClicked(_:)), for: .touchUpInside)
authorNameFilterBtn.addTarget(self, action: #selector(buttonClicked(_:)), for: .touchUpInside)
}
var buttons: [UIButton] { return [bookTitleFilterBtn, authorNameFilterBtn] }
func updateButtonsAppearance(allButtons: [UIButton], selectedButton: UIButton) {
for button in allButtons {
let isSelected = button == selectedButton
let currentTitle = button.currentTitle ?? "-"
let title = NSAttributedString(string: currentTitle, attributes: [.foregroundColor: isSelected ? UIColor.white : UIColor.black])
button.setAttributedTitle(title, for: .normal)
button.backgroundColor = isSelected ? .red : .white
button.isHighlighted = isSelected
}
}
@objc func buttonClicked(_ sender: UIButton) {
DispatchQueue.main.async {
self.updateButtonsAppearance(allButtons: buttons, selectedButton: sender)
}
}
请注意,两个按钮现在都调用相同的 function。 所以现在只有一个真相来源。 如果它在某个地方工作,它在任何地方都可以工作。
您缺少以下代码行来更改另一个按钮的背景颜色。 添加以下代码行。
//left button
@objc func bookTitleFilterBtnClicked(_ sender: UIButton) {
DispatchQueue.main.async {
if self.isHighlighted == false {
....
....
authorNameFilterBtn.backgroundColor = .white
} else {
....
}
}
}
//right button
@objc func authorNameFilterBtnClicked(_ sender: UIButton) {
DispatchQueue.main.async {
if self.isHighlighted == false {
....
bookTitleFilterBtn.backgroundColor = .white
} else {
....
}
}
}
此代码将更改按钮的颜色,反之亦然
设置左键默认选择 StoryBoard
var selectedButton:String = "" // gloable variable
//left button
@objc func bookTitleFilterBtnClicked(_ sender: UIButton) {
if selectedButton != "제목"
{
selectedButton = "제목"
sender.backgroundColor = .red
let title = NSAttributedString(string: "제목", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])//title
sender.setAttributedTitle(title, for: .normal)
sender.isHighlighted = true
self.isHighlighted = true
authorNameFilterBtn.backgroundColor = .white
let title1 = NSAttributedString(string: "작가", attributes: [NSAttributedString.Key.foregroundColor: UIColor.black])//title
authorNameFilterBtn.setAttributedTitle(title1, for: .normal)
authorNameFilterBtn.isHighlighted = false
self.isHighlighted = false
}
}
//right button
@objc func authorNameFilterBtnClicked(_ sender: UIButton) {
if selectedButton != "작가"
{
selectedButton = "작가"
sender.isHighlighted = true
let title = NSAttributedString(string: "작가", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])//Author
sender.setAttributedTitle(title, for: .normal)
sender.backgroundColor = .red
self.isHighlighted = true
bookTitleFilterBtn.isHighlighted = false
self.isHighlighted = false
let title1 = NSAttributedString(string: "제목", attributes: [NSAttributedString.Key.foregroundColor: UIColor.black])
bookTitleFilterBtn.setAttributedTitle(title1, for: .normal)
bookTitleFilterBtn.backgroundColor = .white
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.