gpt4 book ai didi

ios - 在自定义 UITextField 上关闭键盘

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

我正在使用 TextFieldEffects用于自定义 UITextFields。创建文本字段非常简单..

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
let textField = HoshiTextField(frame: textFieldFrame)
textField.placeholderColor = .darkGrayColor()
textField.foregroundColor = .lightGrayColor()

view.addSubView(textField)
}
}

为了关闭键盘,我尝试做我通常做的事情,但不幸的是没有运气。

func textFieldShouldReturn(textField: HoshiTextField) -> Bool {
self.view.endEditing(true)
return false
}

func dismissKeyboard() {
view.endEditing(true)
}

你有什么建议吗?

最佳答案

确认 UITextFieldDelegate 协议(protocol)到您的 ViewController 并分配 textField.delegate = self 然后实现以下委托(delegate)方法:

func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}

为了更好地理解,请参阅我的代码片段 enter image description here

关于ios - 在自定义 UITextField 上关闭键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36001119/

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