0

I have button in which i have set a icon like this :

self.mybutton?.setImage(UIImage(named: "ic_remove_red_eye.png"), for: .normal)

How can i check what is current tint color of this icon and based on that i can change the tint color ?

below is full code of view controller and there is func refresh in which i need to compare the tint color and change it

import UIKit

class LoginViewController: ViewController {

    var eye: UIButton?
    @IBOutlet weak var welcome: UILabel!

    @IBOutlet weak var email: UITextField!

    @IBOutlet weak var password: UITextField!

    @IBOutlet weak var loginButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.eye = UIButton(type: .custom)
        self.eye?.setImage(UIImage(named: "ic_remove_red_eye.png"), for: .normal)
        self.eye?.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 10)
        self.eye?.frame = CGRect(x: CGFloat(password.frame.size.width - 25), y: CGFloat(5), width: CGFloat(25), height: CGFloat(25))
        self.eye?.addTarget(self, action: #selector(self.refresh), for: .touchUpInside)
        password.rightView = self.eye
        password.rightViewMode = .always
        email.layer.masksToBounds = true

        email.layer.borderWidth = 1.2
        email.layer.borderColor = UIColor.lightGray.cgColor

        password.layer.masksToBounds = true

        password.layer.borderWidth = 1.2
        password.layer.borderColor = UIColor.lightGray.cgColor

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func login(_ sender: UIButton) {
    }

    @objc func refresh(){
        print("dd")

        if let tintColor = self.eye?.imageView?.tintColor{
            print(tintColor)}else{
            print("tint color not found ")
        }
    }
    /*
     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */

}
  • 1
    if let tintColor = self.mybutton?.imageView?.tintColor { //use tintColor variable } –  Oct 12 '17 at 21:49
  • i have update the question with full code i m getting nd a tint color how to compare it with my hex string color ocde – feroz siddiqui Oct 12 '17 at 22:07
  • 1
    You can get the RGBA components as an optional array of float values using the following code: tintColor.cgColor.components. These are float values from 0 to 1. First is red, the second is green, the third is blue and the last is the alpha channel. –  Oct 12 '17 at 22:25
  • yes got it. but why my button image tint is not getting changed even after changing the tint color self.eye?.imageView?.tintColor = UIColor.red – feroz siddiqui Oct 12 '17 at 22:33

1 Answers1

1

If you want to check the tint color of the button, based on the documentation the property is mybutton.tintColor of type UIColor. If you really want to get the dominant colors of the UIImage, you could use https://github.com/jathu/UIImageColors

GBaeOne
  • 46
  • 4
  • why the tint color is not changing i dont understand i got mybutton.tintColor how can i compare this hex string of color – feroz siddiqui Oct 12 '17 at 22:11
  • I'm not sure to understand exactly what you want to do but i use this website to get the UIColor of a hex value: http://uicolor.xyz/#/hex-to-ui – GBaeOne Oct 12 '17 at 22:17
  • i m getting this "UIExtendedSRGBColorSpace 0 0.478431 1 1" while printing my tint color how can i compare this value to my hex code which is set as "#bebebe" and if tint color value matches with hexcode how can i change button icon tint color – feroz siddiqui Oct 12 '17 at 22:19
  • Check this post for example about hex and integers: https://stackoverflow.com/questions/27189338/swift-native-functions-to-have-numbers-as-hex-strings –  Oct 12 '17 at 22:36
  • From the website i gave you the UIColor corresponding to you hex #bebebe is UIColor(red:0.75, green:0.75, blue:0.75, alpha:1.0). Then you can just test it like this `let mybutton = UIButton(frame: CGRect.zero) let colorA = mybutton.tintColor let colorB = UIColor(red:0.75, green:0.75, blue:0.75, alpha:1.0) if (colorA == colorB) { print("equal") } else { print("not equal") }` – GBaeOne Oct 12 '17 at 22:37
  • why tint color is not getting set on button image is it becoz i m putting button icon right of a textfield or something have i missed – feroz siddiqui Oct 12 '17 at 22:47