gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:16:45 24 4
gpt4 key购买 nike

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

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

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

下面是 View Controller 的完整代码,有一个函数刷新,我需要在其中比较色调并更改它

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.
}
*/

}

最佳答案

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

关于Swift - 我如何获取按钮图像当前色调颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46719297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com