gpt4 book ai didi

ios - KeyboardWillShowNotification 被调用三次

转载 作者:行者123 更新时间:2023-11-28 06:47:02 26 4
gpt4 key购买 nike

我需要在键盘可见后立即向上移动 UIView。但我现在面临的问题是,当我使用自定义键盘(例如 SwiftKey)时,我的 UIKeyboardWillShowNotification 被调用了三次,这导致动画效果不佳。
有没有办法只处理最后一个通知?我可以轻松躲避第一个,因为高度为 0,但第二个看起来像一个有效高度,我找不到如何解决这个问题的答案。
这是我到目前为止的内容:

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)
}

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)

NSNotificationCenter.defaultCenter().removeObserver(self)
}

func keyboardWillAppear(notification: NSNotification){
print("keyboard appear")
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
print("with height: \(keyboardSize.height)")
if keyboardSize.height == 0.0 {
return
}
self.txtViewBottomSpace.constant = keyboardSize.height
UIView.animateWithDuration(0.4, animations: { () -> Void in
self.view.layoutIfNeeded()
})
}
}

func keyboardWillDisappear(notification: NSNotification){
print("Keyboard disappear")
self.txtViewBottomSpace.constant = 0.0
UIView.animateWithDuration(0.4, animations: { () -> Void in
self.view.layoutIfNeeded()
})
}

我的日志输出是:

keyboard appear
with height: 0.0
keyboard appear
with height: 216.0
keyboard appear
with height: 258.0
Keyboard disappear

那么有没有办法只处理第三个通知而“忽略”前两个?

最佳答案

将以下所有字段设置为 NO 可以解决此问题。

Capitalizaion: None
Correction: No
Smart Dashes: No
Smart insert: No
Smart Quote: No
Spell Checking: No

关于ios - KeyboardWillShowNotification 被调用三次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36033153/

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