简体   繁体   中英

Change tint image color when pressing uibutton in Swift

I have a scrollview inside a view. Inside the scrollview I create programmatically 5 buttons. Every button loads a different image with a different tag each one. I added a function that is called when pressing the buttons.

let avatarsListScrollingView = avatarsListView(CGSizeMake(70.0, 55.0), avatarCount: 5)


func avatarsListView(buttonSize:CGSize, avatarCount:Int) -> UIView {

 **CODE**

    for i in 0...(avatarCount-1)  {
        let button = UIButton(type: .Custom)

        **CODE**

        button.setImage(UIImage(named: avatarsList[i]), forState: .Normal)

        button.tag = i
        button.addTarget(self, action: "avatarListSelected:", forControlEvents: .TouchUpInside)

        avatarButtonView.addSubview(button)
    }
    return avatarButtonView

}

Then when pressing the buttons, I call to "avatarListSelected":

func avatarListSelected(sender:UIButton){

    if let image = sender.imageView?.image?.imageWithRenderingMode(.AlwaysTemplate) {
        sender.setImage(image, forState: .Normal)
        sender.tintColor = UIColor.redColor()
    }
    self.addAvatarView.reloadInputViews()
}

This function tints the image button to red, it is working fine, but the problem is that when I press some other button, I want the other one goes to the original color. Right now every button that I press gets in red.

I tried to add the call to "self.addAvatarView.reloadInputViews()" to try to "redraw" again all the buttons, but never gets called.

Do you guys know some way to do this?

Thanks to everybody! This is the final code that solved the problem:

func avatarListSelected(sender:UIButton){
    print(sender.tag)

    if let image = sender.imageView?.image?.imageWithRenderingMode(.AlwaysTemplate) {
        sender.setImage(image, forState: .Normal)
        sender.tintColor = UIColor.redColor()
    }

    for view in self.avatarButtonView.subviews as [UIView] {
        if let btn = view as? UIButton {
            if btn.tag != sender.tag  {
                btn.setImage(UIImage(named: avatarsList[btn.tag]), forState: .Normal)
            }
        }
    }
}

Create a property selectedButton: UIButton? and keep a reference to the selected button there. Don't forget to update it in avatarListSelected method and before you change it, if it isn't nil, change its color to original (and then change it).
If the buttons have different original colors, subclass UIButton class and keep the original color there.

I don't know if is better approach or answer, but, i maybe could delivery this using this approach:

Create a method that will "fill" the color for your choice button and "clear" color to others , but its a method that loop through UIScrollView and look for each UIButton. Something like this :

func setBackgroundColorButton(color:UIColor , buttonTag:Int){
   for view in self.scrollView.subviews as [UIView] {
      if let btn = view as? UIButton {
         if btn == buttonTag  {
             btn.tintColor = color
         } else {
             btn.tintColor = UIColor.whiteColor()
         }
      }
   }
}

This is the concept, i didn't tested, but maybe just need adjust to search inside your scroll view or similar.

But with this will be work nice i believe :D

You could do it like this

for i in 0...5 {
      let button = UIButton(type: .Custom)
      let x = 50 * i + 10
      let y = 50
      button.frame = CGRectMake(CGFloat(x), CGFloat(y), 40, 40)
      button.setTitle("\(i)", forState: .Normal)
      button.tag = i
      button.backgroundColor = UIColor.greenColor()
      button.tintColor = UIColor.blueColor()
      button.addTarget(self, action: "avatarListSelected:", forControlEvents: .TouchUpInside)

     self.view.addSubview(button)
}

func avatarListSelected(sender : UIButton){
        sender.backgroundColor = UIColor.orangeColor()

        for view in self.view.subviews{
            if(view.isKindOfClass(UIButton)){
                let button = view as! UIButton
                if button.tag != sender.tag{
                    button.backgroundColor = UIColor.greenColor()
                }
            }
        }
    }

The frame etc is just for demonstation purpose only, you should of course use your own value. The tintColor property is not valid for all button types. Read the documentation for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM