我使用UITextfied
,当我点击文本输入框时键盘会出现,但是当我按下return键时,键盘不会消失。我使用了以下代码:
func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
{
return true;
}
方法 resignFirstResponder 在函数中没有起作用。
我使用UITextfied
,当我点击文本输入框时键盘会出现,但是当我按下return键时,键盘不会消失。我使用了以下代码:
func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
{
return true;
}
方法 resignFirstResponder 在函数中没有起作用。
import Foundation
import UIKit
// Don't forget the delegate!
class ViewController: UIViewController, UITextFieldDelegate {
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@IBOutlet var myTextField : UITextField?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.myTextField.delegate = self;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldShouldReturn(textField: UITextField!) -> Bool {
self.view.endEditing(true);
return false;
}
}
@IBAction func textFieldDoneEditing(sender: UITextField) {
sender.resignFirstResponder()}
在项目导航器中选择Main.storyboard,然后打开连接检查器。从“Did End On Exit”旁边的圆圈拖动到故事板中的黄色视图控制器图标上,然后释放。一个小弹出菜单将出现,其中包含一个动作的名称,这是我们刚刚添加的动作。单击textFieldDoneEditing动作以选择它,就完成了。
使用来自UITextFieldDelegate
的可选函数。
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.endEditing(false)
}
false
表示该字段可以要求辞职。true
- 强制辞职。
Swift 3
将以下代码添加到您的 VC 中
//hide keyboard when user tapps on return key on the keyboard
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true);
return false;
}
对我来说可以
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(handleScreenTap(sender:)))
self.view.addGestureRecognizer(tap)}
func handleScreenTap(sender: UITapGestureRecognizer) {
self.view.endEditing(true)
}
确保您的textField代理已设置为编写textfield相关代码的视图控制器。
self.textField.delegate = self
你可以将此放置在任何地方,但不要放在UIButton中。
func TextFieldEndEditing(text fiend name: UITextField!) -> Bool
{
return (false)
}
self.view.endEditing(true)
在您使用的视图控制器中:
//suppose you are using the textfield label as this
@IBOutlet weak var emailLabel: UITextField!
@IBOutlet weak var passwordLabel: UITextField!
//then your viewdidload should have the code like this
override func viewDidLoad() {
super.viewDidLoad()
self.emailLabel.delegate = self
self.passwordLabel.delegate = self
}
//then you should implement the func named textFieldShouldReturn
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
// -- then, further if you want to close the keyboard when pressed somewhere else on the screen you can implement the following method too:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true);
}
你应该将UITextfied与视图控制器的委托连接起来,以调用此功能。
一键隐藏键盘并在打开键盘时移动视图:Swift 5
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(taped))
view.addGestureRecognizer(tap)
NotificationCenter.default.addObserver(self, selector: #selector(KeyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(KeyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}
@objc func taped(){
self.view.endEditing(true)
}
@objc func KeyboardWillShow(sender: NSNotification){
let keyboardSize : CGSize = ((sender.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size)!
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= keyboardSize.height
}
}
@objc func KeyboardWillHide(sender : NSNotification){
let keyboardSize : CGSize = ((sender.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size)!
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += keyboardSize.height
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}