1
votes

If my UIbutton is selected :

     let image = UIImage(named: "photo-camera")?.imageWithRenderingMode(.AlwaysTemplate)
     btnPhoto.setImage(image, forState: .Normal)
     btnPhoto.tintColor = UIColor.orange()

If my UIbutton is not selected:

    let image = UIImage(named: "photo-camera")?.imageWithRenderingMode(.AlwaysTemplate)
    btnPhoto.setImage(image, forState: .Normal)
    btnPhoto.tintColor = UIColor.grayColor()

Now, my issue is: when UIButton is not selected everything is displayed fine: enter image description here Instead when the UIButton is selected that is the result:enter image description here

The tint color doesn't change the image but the background. I can't understand why.

3
You want to change the color of your logo when it is selected ? - GaétanZ
Yes only the logo - user4385051
You can add an imageView above your button. - Khuong
how do you make a difference on the button being selected or not? Can you show that code? Also, the creation of btnPhoto variable. - dirtydanee

3 Answers

-1
votes

Just set image for selected state, and set tint color to clear.

-1
votes

Take btnPhoto with type custom

let btnPhoto = UIButton(type: .Custom)
let image = UIImage(named: "image_name")?.imageWithRenderingMode(.AlwaysTemplate)
btnPhoto.setImage(image, forState: .Normal)
btnPhoto.tintColor = UIColor.grayColor()

btnPhoto.addTarget(self, action: “btntSelected:", forControlEvents: .TouchUpInside)

func btnSelected(sender : UIButton)
{
var btn:UIButton = sender
btn.tintColor = UIColor.orange()
}
-1
votes

You have to setBackgroundImage, not setImage.

class CustomTintButton: UIButton {

override public var isSelected: Bool{
    didSet {
        if self.isSelected {
            self.tintColor = .orange
        }
        else{
            self.tintColor = .gray
        }

      }
}