我基于数组计数以编程方式在scrollview上添加了多个UIButton,直到这个工作正常。要求只选择按钮文字标题颜色应改为棕色,保持蓝色。我无法解决这一个请使用Swift 3的一些帮助。我只需要选择或点击按钮文本标题颜色需要更改保持所有都是其余的颜色。
for titles in arrayPageTitles {
var titleSize: CGSize = (titles as AnyObject).size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14.0)])
titleSize = CGSize(width: CGFloat(ceilf(Float(titleSize.width))), height: CGFloat(ceilf(Float(titleSize.height))));
/**
creating top page strip tab Buttons
*/
buttonTitle = UIButton(type: UIButtonType.custom)
buttonTitle = UIButton(frame: CGRect(x: buttonPositionX + 5, y: 10, width: titleSize.width+40, height: (scrollViewTop?.frame.size.height)! - 20))
buttonTitle.setTitle(titles, for: .normal)
buttonTitle.setTitleColor(UIColor.blue, for: .normal)
buttonTitle.setTitleColor(UIColor.white, for: .selected
buttonTitle.clipsToBounds = true
buttonTitle.layer.masksToBounds = true
buttonTitle.layer.cornerRadius = 17
buttonTitle.layer.borderWidth = 1
buttonTitle.layer.borderColor = UIColor.darkGray.cgColor
buttonTitle.tag = increaseIndex
buttonTitle.addTarget(self, action: #selector(buttonTitleAction), for: .touchUpInside)
arrayTitleButtons .add(buttonTitle)
scrollViewTop?.addSubview(buttonTitle)
let currentButtonX:CGFloat = buttonPositionX
let buttonWidth:CGFloat = buttonTitle.frame.size.width
buttonPositionX = currentButtonX + buttonWidth + 10
if arrayPageTitles.first == titles {
viewLine.frame = CGRect(x: buttonTitle.frame.origin.x, y: buttonTitle.frame.origin.y, width: buttonTitle.frame.size.width, height: buttonTitle.frame.size.height)
viewLine.layer.cornerRadius = 17
viewLine.clipsToBounds = true
viewLine.layer.masksToBounds = true
viewLine.backgroundColor = UIColor.red
scrollViewTop?.addSubview(viewLine)
scrollViewTop?.addSubview(buttonTitle) // need to fix ... this add only first button
buttonTitle.setTitleColor(UIColor.white, for: .normal)
}
/**
creating collection views
*/
let storyBoard = UIStoryboard(name:"Main", bundle:nil)
let pageStripTypeViewController = storyBoard.instantiateViewController(withIdentifier: "kPageStripVC") as! FirstViewController
...
答案 0 :(得分:1)
您可以在@IBAction中更改发件人的选择状态,如下所示:
@IBAction func buttonAction(_ sender: UIButton) {
// create an array of buttons
var mybuttonArray: [UIButton] = view.subviews.filter{$0 is UIButton} as! [UIButton]
// filter it down to buttons that aren't the sender
mybuttonArray = mybuttonArray.filter{$0 != sender}
// figure out of it's selected
mybuttonArray.forEach({ button in
if button.isSelected {
// unselected it
button.isSelected = false
}
})
// select the sender's button
sender.isSelected = !sender.isSelected
// whatever else you need to
}
答案 1 :(得分:1)
创建选择器时需要传递按钮的引用,以便可以与其他按钮进行比较并更改isSelected状态
buttonTitle.addTarget(self, action: #selector(ViewController.buttonTitleAction(_:)), for: .touchUpInside)
//将ViewController替换为您的ViewController名称
func buttonTitleAction(_ sender: UIButton) {
arrayTitleButtons.forEach { (button) in
if button == sender {
button.isSelected = true
sender.setTitleColor(UIColor.brown, for: .selected)
} else {
button.isSelected = false
}
}
}