Swift - 如何获取按钮图像当前色调颜色

时间:2017-10-12 21:39:24

标签: swift

我有按钮,我在其中设置了这样的图标:

self.mybutton?.setImage(UIImage(名称:“ic_remove_red_eye.png”),用于:.normal)

如何检查此图标的当前色调颜色,并根据我可以更改色调颜色?

下面是视图控制器的完整代码,并且有func刷新,我需要比较色调颜色并更改它

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 个答案:

答案 0 :(得分:1)

如果要检查按钮的色调颜色,则根据文档属性为UIColor类型的mybutton.tintColor。 如果你真的想要获得UIImage的主色,你可以使用https://github.com/jathu/UIImageColors