如何检测何时显示和隐藏键盘

我怎样才能检测到键盘显示和隐藏从我的应用程序?

在您的类的ViewDidLoad方法中设置为侦听有关键盘的消息:

// Listen for keyboard appearances and disappearances [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; 

然后在你指定的方法(在这种情况下, keyboardDidShowkeyboardDidHide ),你可以做一些事情:

 - (void)keyboardDidShow: (NSNotification *) notif{ // Do something here } - (void)keyboardDidHide: (NSNotification *) notif{ // Do something here } 

Swift 4:

 override func viewWillAppear(_ animated: Bool) { NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: Notification.Name.UIKeyboardWillHide, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear), name: Notification.Name.UIKeyboardWillShow, object: nil) } @objc func keyboardWillAppear() { //Do something here } @objc func keyboardWillDisappear() { //Do something here } override func viewWillDisappear(_ animated: Bool) { NotificationCenter.default.removeObserver(self) } 

迅速:

 override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillAppear:", name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillDisappear:", name: UIKeyboardWillHideNotification, object: nil) } func keyboardWillAppear(notification: NSNotification){ // Do something here } func keyboardWillDisappear(notification: NSNotification){ // Do something here } 

编辑:
你可能也想添加这段代码。 这可以防止在更改视图时发生罕见的崩溃。

 override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) NSNotificationCenter.defaultCenter().removeObserver(self) } 

Swift 3:

 NotificationCenter.default.addObserver(self, selector: #selector(viewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(viewController.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) func keyboardWillShow(_ notification: NSNotification){ // Do something here } func keyboardWillHide(_ notification: NSNotification){ // Do something here } 

查看“文本,Web和编辑编程指南”中的“ pipe理键盘”部分,以获取有关跟踪正在显示或隐藏的键盘以及如何手动显示/解除键盘的信息。

您需要注册2个键盘通知:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil]; 

关于如何调整你的TextField的键盘 – 伟大的职位 – http://iosdevelopertips.com/user-interface/adjust-textfield-hidden-by-keyboard.html

Swift 4:

  NotificationCenter.default.addObserver( self, selector: #selector(ControllerClassName.keyboardWillShow(_:)), name: Notification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(ControllerClassName.keyboardWillHide(_:)), name: Notification.Name.UIKeyboardWillHide, object: nil) 

接下来,添加方法来停止侦听对象生命结束时的通知:

 Then add the promised methods from above to the view controller: deinit { NotificationCenter.default.removeObserver(self) } func adjustKeyboardShow(_ open: Bool, notification: Notification) { let userInfo = notification.userInfo ?? [:] let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue let height = (keyboardFrame.height + 20) * (open ? 1 : -1) scrollView.contentInset.bottom += height scrollView.scrollIndicatorInsets.bottom += height } @objc func keyboardWillShow(_ notification: Notification) { adjustKeyboardShow(true, notification: notification) } @objc func keyboardWillHide(_ notification: Notification) { adjustKeyboardShow(false, notification: notification) } 

如果你有一个以上的UITextField ,你需要在键盘出现或消失的时候(或之前)做一些事情,你可以实现这个方法。

UITextFieldDelegate添加到您的类中。 分配整数计数器,让我们说:

 NSInteger editCounter; 

将这个计数器设置为0,在viewDidLoad某处。 然后,实现textFieldShouldBeginEditingtextFieldShouldEndEditing委托方法。

在第一个添加1到editCounter。 如果editCounter的值变为1 – 这意味着键盘将出现(如果您返回YES)。 如果editCounter> 1 – 这意味着键盘已经可见,另一个UITextField保存焦点。

textFieldShouldEndEditing中,从editCounter中减去1。 如果你得到零 – 键盘将被解雇,否则将保持在屏幕上。

你可以使用KBKeyboardObserver库。 它包含一些例子,并提供简单的界面。

Swift 4dd 20 october 2017

 override func viewDidLoad() { [..] NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear(_:)), name: Notification.Name.UIKeyboardWillHide, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(_:)), name: Notification.Name.UIKeyboardWillShow, object: nil) } @objc func keyboardWillAppear(_ notification: NSNotification) { if let userInfo = notification.userInfo, let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue).cgRectValue { let inset = keyboardFrame.height // if scrollView is not aligned to bottom of screen, subtract offset scrollView.contentInset.bottom = inset scrollView.scrollIndicatorInsets.bottom = inset } } @objc func keyboardWillDisappear(_ notification: NSNotification) { scrollView.contentInset.bottom = 0 scrollView.scrollIndicatorInsets.bottom = 0 } deinit { NotificationCenter.default.removeObserver(self) } 
Interesting Posts