本文介绍了keyboardWillShow火两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到问题"keyboardWillShow"触发两次,但是"keyboardWillHide"调用一次.

I'm stuck with problem "keyboardWillShow" fires twice, but the "keyboardWillHide" called once.

这是一个示例,其中我将尽快打印键盘尺寸如"keyboardWillShow"触发.我还将断点放在"viewDidLoad"中,并且观察者仅注册一次.我添加了两个元素"UITextField"和"UITextView",这两个元素的行为相同.

Here is an example where I'm printing the keyboard sizes as soon as "keyboardWillShow" fires.I've also put breakpoint in "viewDidLoad" and the observer registers only once.I've added two elements "UITextField" and "UITextView" and for both it is the same behaviour.

我正在使用iOS 9.2,swift lang.,xcode 7

I'm using iOS 9.2, swift lang., xcode 7

在我的ViewController下面

Below my ViewController

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
        print("keyboardWillShow sizes: \(keyboardSize)")
    }

}

func keyboardWillHide(notification: NSNotification) {
    print("HideHideHide")
}

}

更新

第一次使用大小触发一次:keyboardWillShow大小:(0.0、568.0、320.0, 253.0 )

First time it fires once with sizes: keyboardWillShow sizes: (0.0, 568.0, 320.0, 253.0)

其余两次以不同的大小:(第二个y位置也改变了,高度也改变了)keyboardWillShow大小:(0.0、568.0、320.0, 216.0 )keyboardWillShow大小:(0.0, 352.0 ,320.0, 216.0 )

for the rest it twice with different sizes:(second y position is changed also the height changed)keyboardWillShow sizes: (0.0, 568.0, 320.0, 216.0)keyboardWillShow sizes: (0.0, 352.0, 320.0, 216.0)

推荐答案

问题已连接到模拟器在真实设备上,它会按预期触发一次.

Issue is connected to simulator On the real device it fires once as supposed to be.

这篇关于keyboardWillShow火两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 08:53