gpt4 book ai didi

ios - 选定/单击的 UIButton 文本标题颜色需要以编程方式从 ScrollView 上的按钮数量更改

转载 作者:行者123 更新时间:2023-11-28 12:22:25 24 4
gpt4 key购买 nike

我根据数组计数以编程方式在 scrollview 上添加了 UIButtons 的数量,直到它工作正常为止。要求只是选中的按钮文字标题颜色应该变成棕色,保持为蓝色。我无法解决这个问题,请有人帮助使用 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

....

最佳答案

您可以在您的@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
}

关于ios - 选定/单击的 UIButton 文本标题颜色需要以编程方式从 ScrollView 上的按钮数量更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44411719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com