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

Ios 选定/单击的UIButton文本标题颜色需要以编程方式从scrollview上的按钮数更改,ios,swift,uiscrollview,uibutton,Ios,Swift,Uiscrollview,Uibutton,我根据数组计数以编程方式在scrollview上添加了许多UIButton,直到这一点工作正常为止。要求只选择按钮文本标题颜色应改为棕色,其余为蓝色。我无法解决这个问题,请使用Swift 3寻求帮助。我只需要选择或点击按钮文本标题的颜色需要改变,所有的都是剩余的颜色 for titles in arrayPageTitles { var titleSize: CGSize = (titles as AnyObject).size(attributes: [N

我根据数组计数以编程方式在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

..

您可以在@iAction中更改发件人的选择状态,如下所示:

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

您可以在@iAction中更改发件人的选择状态,如下所示:

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

创建选择器时需要传递按钮的引用,以便与其他按钮进行比较并更改isSelected状态

buttonTitle.addTarget(self,action:#选择器(ViewController.buttonTitle action(:)),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
           }
        }
  }

创建选择器时需要传递按钮的引用,以便与其他按钮进行比较并更改isSelected状态

buttonTitle.addTarget(self,action:#选择器(ViewController.buttonTitle action(:)),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
           }
        }
  }


显示用于在ScrollView中添加按钮的代码实际上我需要集成页面条模式。除了所选的按钮外,一切正常。如果您需要帮助,则需要向我们显示您当前正在尝试的内容ButtonTile=UIButton(类型:UIButtonType.custom)ButtonTile=UIButton(框架:cRect(x:buttonPositionX+5,y:10,宽度:titleSize.width+40,高度:(scrollViewTop?.frame.size.height)!-20))ButtonTile.setTitle(titles,表示:.normal)ButtonTile.setTitleColor(UIColor.blue,表示:.normal)ButtonTile.setTitleColor(UIColor.white,表示:.highlighted)ButtonTile.tag=increaseIndex scrollViewTop?.addSubview(ButtonTile)不在注释中添加代码,它很难从评论中读取代码,编辑您的问题并在其中添加代码。如何在ScrollView中添加按钮的代码实际上我需要集成页面条模式。除了所选的按钮外,一切正常。如果您需要帮助,则需要向我们显示您当前正在尝试的内容ButtonTile=UIButton(类型:UIButtonType.custom)ButtonTile=UIButton(框架:cRect(x:buttonPositionX+5,y:10,宽度:titleSize.width+40,高度:(scrollViewTop?.frame.size.height)!-20))ButtonTile.setTitle(titles,表示:.normal)ButtonTile.setTitleColor(UIColor.blue,表示:.normal)ButtonTile.setTitleColor(UIColor.white,表示:.highlighted)ButtonTile.tag=increaseIndex scrollViewTop?.addSubview(ButtonTile)不在注释中添加代码,从评论中读取代码、编辑您的问题以及在itI中添加代码都很困难。我尝试过这种选择方法,但效果并不准确。首先我选择Button3,Button3标题文本的颜色会改变,但当我选择Button5时,Button3和Button5标题文本的颜色都是相同的。我能给出完整的代码吗?除了这个,所有东西都可以正常工作,请帮助我如果我阅读正确,你所做的就是根据控件状态改变标题文本的颜色。你已经在你的设置代码中这样做了,所以你需要做的就是跟踪选择了哪些按钮。好的,我会再试一次谢谢它的工作。。。问题出在ButtonTile.setTitleColor(UIColor.brown,用于:。选定项)中。我将其更改为button.titleLabel?.textColor=UIColor.brown,现在它可以正常工作。我尝试了此选择方法,但效果不准确。首先我选择Button3,Button3标题文本的颜色会改变,但当我选择Button5时,Button3和Button5标题文本的颜色都是相同的。我能给出完整的代码吗?除了这个,所有东西都可以正常工作,请帮助我如果我阅读正确,你所做的就是根据控件状态改变标题文本的颜色。你已经在你的设置代码中这样做了,所以你需要做的就是跟踪选择了哪些按钮。好的,我会再试一次谢谢它的工作。。。问题出在ButtonTile.setTitleColor(UIColor.brown,for:.selected)中,我将其更改为button.titleLabel?.textColor=UIColor.brown,现在它正在工作,我需要感谢的是哪个部分不工作?尝试放置断点并检查条件是否工作?它表示arrayTitleButtons为空,并退出forEach条件。现在是最后一次了,只有这个问题,你能帮我吗。我可以发送代码你可以给邮件ID为什么arrayTitleButtons是空的,你正在向arrayTitleButtons添加Buttontile右侧,尝试添加此行arrayTitleButtons.append(按钮标题)在行scrollViewTop?之前。addSubview(Buttontile)其工作状态现在我已更改为Buttontile.titlelabel.TextColor哪个部分不工作?尝试放置断点并检查条件是否工作?它表示arrayTitleButtons为空,并退出forEach条件。现在是最后一次了,只有这个问题,你能帮我吗。我可以发送代码你可以给邮件ID为什么arrayTitleButtons是空的,你正在向arrayTitleButtons添加Buttontile右侧,尝试添加此行arrayTitleButtons.append(按钮标题)在行scrollViewTop?之前。addSubview(Buttontile)其工作方式现在我改为Buttontile.titlelabel.textColor