我有一个UIViewController
,里面有一个UITextField
,当我单击离开或视图被关闭时,我试图关闭键盘。但是,当我打电话给resignFirstResponder()
时,键盘仍然没有关闭,我不太确定为什么。这是我的代码:
class MessageViewController: UIViewController, UITextFieldDelegate {
var messageTextField : UITextField = UITextField()
override func viewDidLoad() {
...
messageTextField.frame = CGRectMake(10, UIScreen.mainScreen().bounds.size.height-50, UIScreen.mainScreen().bounds.size.width-80, 40)
messageTextField.delegate = self
messageTextField.borderStyle = UITextBorderStyle.Line
messageTextField.becomeFirstResponder()
self.view.addSubview(messageTextField)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
...
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
println("Touched")
}
func keyboardWillShow(notification: NSNotification) {
var keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey]as? NSValue)?.CGRectValue()
let messageFrame = messageTextField.frame
let newY = messageFrame.origin.y - keyboardSize!.height
messageTextField.frame = CGRectMake(messageFrame.origin.x, newY, messageFrame.size.width, messageFrame.size.height)
}
func keyboardWillHide(notification: NSNotification) {
var keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey]as? NSValue)?.CGRectValue()
let messageFrame = messageTextField.frame
let newY = messageFrame.origin.y - keyboardSize!.height
messageTextField.frame = CGRectMake(messageFrame.origin.x, newY, messageFrame.size.width, messageFrame.size.height)
}
}
有谁知道为什么键盘不关闭?我以编程方式将 UITextField 添加到视图中,而不是使用情节提要。这有什么不同吗?
class ViewController: UIViewController,UITextFieldDelegate {
确认协议
messageTextField.delegate=self
设置委托
func textFieldShouldReturn(textField: UITextField!) -> Bool
{
messageTextField.resignFirstResponder()
return true;
}
使用此代码..
以下是我如何解决这个问题。我希望这种方法能解决你的问题。
文本字段
@IBOutlet var userID : UITextField!
功能。
func textFieldShouldReturn(textField: UITextField!)-> Bool
{ userID.resignFirstResponder( )
return true
}
在所需的函数中,您需要编写此语法。
@IBAction func login(sender: AnyObject)
{
userID.resignFirstResponder( )
}
这是在ViewDidLoad或ViewDidAppear中
override func viewDidLoad( )
{
userID.delegate = self;
}
我无法评论以前的用户。这就是我写这篇文章的原因。我希望这能给你带来想法。
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.view.endEditing(true)
//textField.resignFirstResponder()
return false
}
检查delegate
添加到您的viewDidLoad()
self.yourTextField.delegate = self;
如何消除 UI文本字段键盘在您的 SWIFT 应用程序中
这可能有助于您:)
@vijeesh的答案可能会起作用,逻辑几乎是正确的,但是如果你使用多个UITextField,它在技术上是错误的。 textField 是调用 textFieldShouldReturn 时传递的 UITextField 参数。问题是,你只是在声明:
textField.resignFirstResponder()
你的程序不知道你指的是什么UITextField。即使你的程序中可能只有一个textField,并且你知道它是你的messageTextField,程序仍然不知道这一点。它只看到"textField"。所以你必须告诉它对程序中的每个UITextField做什么。即使你只有一个。这应该有效:
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == messageTextField {
messageTextField.resignFirstResponder()
}
return true
}
非常简单。首先添加点按手势
var tap = UITapGestureRecognizer(target: self, action: "handle:")
view.addGestureRecognizer(tap)
在函数句柄中
func handle(tap: UITapGestureRecognizer){
view.endEditing(true)
}
因此,您可以在单击UITextField外部时关闭键盘