gpt4 book ai didi

ios - keyboardWillShow 开火两次

转载 作者:搜寻专家 更新时间:2023-10-31 23:00:49 25 4
gpt4 key购买 nike

我遇到了“keyboardWillShow”触发两次,但“keyboardWillHide”调用一次的问题。

这里是 an example一旦“keyboardWillShow”触发,我就会在其中打印键盘尺寸。我还在“viewDidLoad”中放置了断点,观察者只注册了一次。我添加了两个元素“UITextField”和“UITextView”,两者的行为相同。

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

在我的 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)

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

最佳答案

可能您订阅了多个 UIKeyboardWillShowNotification 而忘记取消订阅。

尝试在 viewWillAppear 中添加观察者,并在 viewWillDisappear 中移除观察者。

关于ios - keyboardWillShow 开火两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35696344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com