我在程序中添加了一个水平的UIScrollView
按钮。我必须改变用户选择按钮的颜色。但如果用户选择另一个按钮,则必须将上一个按钮颜色更改为默认颜色。 我该怎么做?请帮我...如何按下一个按钮时更改上一个按钮的颜色?
func createHorizontalScroll()
{
let scrollView = UIScrollView(frame: CGRect(x: CGFloat(0), y: CGFloat(410), width: CGFloat(view.frame.size.width), height: CGFloat(40)))
var buttonX: CGFloat = 0
for index in 0..<btnNames.count
{
//add an element and the previous element together
let sum = btnNames[index]
button = UIButton(frame: CGRect(x: CGFloat(buttonX), y: CGFloat(0), width: CGFloat(100), height: CGFloat(40)))
print("btnNames:\(sum)")
button.setTitle("\(sum)",for:.normal)
button.layer.borderWidth = 2.5
button.layer.borderWidth = 2.5
button.layer.borderColor = UIColor.white.cgColor
button.layer.backgroundColor = UIColor.black.cgColor
button.tag = index
scrollView.addSubview(button)
buttonX = button.frame.size.width + buttonX
button.addTarget(self, action: #selector(changeView), for: .touchUpInside)
}
scrollView.contentSize = CGSize(width: CGFloat(buttonX), height: CGFloat(scrollView.frame.size.height))
scrollView.backgroundColor = UIColor.clear
view.addSubview(scrollView)
}
func changeView(_ sender: UIButton)
{
print("I Clicked a button \(Int(sender.tag))")
}
请发表您的代码 –
我说我的源代码在这里。我必须更改changeView功能中的按钮颜色 – krishna
按钮标签或标题在整个应用程序中保持相同? –