gpt4 book ai didi

Swift onclick 文本字段图像

转载 作者:行者123 更新时间:2023-11-30 12:42:47 25 4
gpt4 key购买 nike

我的 Swift 代码中有一个文本字段

let PassINP: UITextField = {
let textField = UITextField()

textField.rightViewMode = .always
var imageR = UIImageView(frame: CGRect(x:7,y:4,width:24,height:27))
imageR.image = #imageLiteral(resourceName: "eye")
var paddingR = UIView(frame: CGRect(x: 0, y: 0, width:40, height: 32))
paddingR.addSubview(imageR)
textField.rightView = paddingR

textField.isSecureTextEntry = true
return textField
}()

我想在单击 rightView 时调用一个函数。我怎样才能实现这一点?
我尝试添加此代码

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.rightViewTapped))
paddingR.addGestureRecognizer(tapGesture)

但它不起作用

最佳答案

为此,您需要将 TapGesture 添加到 UITextFieldrightView

//Add UITapGestureRecognizer to your PassINP's rightView `viewDidLoad`
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(favoriteButtonPressed))
PassINP.rightView?.addGestureRecognizer(tapGesture)

现在添加 rightViewTapped 方法来处理 rightView 上的点击

func rightViewTapped(_ gesture: UITapGestureRecognizer) {
print("rightViewTapped")
}

关于Swift onclick 文本字段图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42036788/

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