选择/单击的UIBUTTON文本标题颜色需要从编程中从滚动浏览中的按钮数进行更改



我根据数组计数以编程方式添加了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
}

创建选择器时需要传递按钮的引用

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
           }
        }
  }

最新更新