用swift编程语言隐藏文本字段的键盘
我在Objective-C方面没有什么经验。 我想用Swift编程语言来隐藏文本字段的键盘。
我也试过这个
func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore. { return true; }
但是当我打回来时,这个方法并没有被触发。 任何人都有这个运气?
我认为问题是设置代表。
请将textfield Delegate设置为像这样的ViewController
class ViewController: UIViewController,UITextFieldDelegate {
然后像这样为您的文本字段创buildIBOutlet
@IBOutlet var txtTest : UITextField = nil
确保它连接到您的故事板视图上的文本字段。
最后使用设置其代表
txtTest.delegate=self
我们差不多完成了。 现在把这个委托function放到你的class上。
func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore. { textField.resignFirstResponder() return true; }
希望这将解决您的问题。
只需重写名为“touchesBegan”的UIViewController方法,并将endEditing设置为true即可。 像这样:
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) { self.view.endEditing(true) }
为了在按下button时隐藏键盘,您需要使用以下function。
self.view.endEditing(true)
这在按下button时有效。
希望这可以帮助那里的人。
开始了:
- 添加一个textField到你的视图
- 创build一个新的Swift文件
- 将此新文件设置为该特定视图的类
- 添加一个TextField到你的视图
- 为文本字段创build一个Outlet(我的名字是“txtField”!)
-
用下面的代码replaceSwift类文件中的任何代码:
import Foundation import UIKit //01 create delegation class MyViewController2: UIViewController,UITextFieldDelegate { @IBOutlet weak var txtField: UITextField!=nil override func viewDidLoad() { super.viewDidLoad() // additional setup after loading the view //02 set delegate to textfield txtField.delegate = self } //03 textfield func for the return key func textFieldShouldReturn(textField: UITextField) -> Bool { txtField.resignFirstResponder() return true; } //textfield func for the touch on BG override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { txtField.resignFirstResponder() self.view.endEditing(true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() //dispose of any resources that can be recreated } }
- 试试看,快乐,说谢谢!
-
将UITextFieldDelegate添加到类声明中:
类ViewController:UIViewController,UITextFieldDelegate
-
连接文本字段或编程写入
@IBOutlet弱var userText:UITextField!
-
设置你的视图控制器为文本字段委托在视图中加载:
override func viewDidLoad() { super.viewDidLoad() self.userText.delegate = self }
-
添加以下function
func textFieldShouldReturn(userText: UITextField!) -> Bool { userText.resignFirstResponder() return true; }
所有这一切你的键盘将开始通过触摸外面的文本字段以及按回车键解雇。
像@spfursich说的那样,最好的方法是,当用户触摸键盘上方的任何位置时,键盘将会消失。 这里是代码:
override func viewDidLoad() { super.viewDidLoad() var tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "DismissKeyboard") self.view.addGestureRecognizer(tap) } func DismissKeyboard(){ self.view.endEditing(true) }
以简单的方式隐藏键盘只是覆盖UIView“touchBegan方法”。 所以当你点击视图键盘的任何地方时都是隐藏的。 这里是示例代码..(Swift 3.0更新代码)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { self.view.endEditing(true) }
首先,您需要为您的文本字段设置delegate
,那么您需要包含resignFirstResponder()
以隐藏键盘时按下keybaord的返回button。
func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore. { textField .resignFirstResponder() return true; }
你可以试试这个代码来让“返回”键执行代码。
func textFieldShouldReturn(textField: UITextField) -> Bool{ textField.resignFirstResponder() performAction() return true; } func performAction(){ //execute code for your action inside this function }
希望这可以帮到你。
您也可以这样做:从TextField的连接检查器添加事件“退出时结束”,并将其连接到相关ViewController的方法
在我的例子中,我将它连接到名为“已结束”
将发件人声明为UITextField,然后调用sender.resignFirstResponder(); 喜欢这个:
@IBAction func ended (sender: UITextField){ sender.resignFirstResponder(); }
在这里我已经解决了这个问题,当键盘打开,视图被隐藏在键盘后面 时,我们需要dynamic地改变UI约束 ,以使整个视图可见。 在我的情况下,我正在dynamic地改变UITextField
的bottonConstraint。
所以,这里是完整的代码。 我有一个UITextView
和UITextField
在UIViewController
工作,只是testing…
import UIKit class ViewController: UIViewController, UITextViewDelegate, UITextFieldDelegate { @IBOutlet weak var textView: UITextField! @IBOutlet weak var textField: UITextView! /*Height Constraint for textField*/ @IBOutlet weak var bottom: NSLayoutConstraint! var defaultHeight:CGFloat = 0.0; override func viewDidLoad() { super.viewDidLoad() defaultHeight = bottom.constant; NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil); NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil); } override func resignFirstResponder() -> Bool { textView.resignFirstResponder(); textField.resignFirstResponder(); return true; } override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { resignFirstResponder() print("touched began") } func textViewDidEndEditing(textView: UITextView) { resignFirstResponder() print("text view did end editing") } func textViewShouldEndEditing(textView: UITextView) -> Bool { resignFirstResponder() print("text view should end editing") return true; } func textFieldShouldReturn(textField: UITextField) -> Bool { print("text field should return") resignFirstResponder() return true; } func textFieldDidEndEditing(textField: UITextField) { resignFirstResponder() print("did end editing") } func keyboardWillShow(notification: NSNotification) { print("keyboard will show.............") if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() { let a = keyboardSize.height; self.bottom.constant = a + defaultHeight; self.view.updateConstraints(); } } func keyboardWillHide(nofication:NSNotification) { print("keyboard will hide................") bottom.constant = defaultHeight; self.view.updateConstraints(); } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
UIApplication.sharedApplication().sendAction(#selector(UIResponder.resignFirstResponder), to:nil, from:nil, forEvent:nil)
现在在Swift 3中,最简单的方法是
方法1:按下“返回”键时调用。
func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField1.resignFirstResponder() textField2.resignFirstResponder() return true; }
方法2:按下“返回”键时调用。
func textFieldShouldReturn(_ textField: UITextField) -> Bool { self.view.endEditing(true) return true; }
方法3:全局方法写在视图中加载
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))
注意:不要忘记添加这个。 其他明智的不工作。
UIGestureRecognizerDelegate, UITextFieldDelegate
我希望它会为你工作:)