UIKeyboardWillShowNotification被调用了三次 [英] UIKeyboardWillShowNotification is called three times

查看:302
本文介绍了UIKeyboardWillShowNotification被调用了三次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to move a UIView up as soon as the keyboard will become visible. But the problem I'm facing right now is that my UIKeyboardWillShowNotification is called three times when I'm using a custom Keyboard (e.g. SwiftKey) which results in a bad animation.
Is there a way to handle only the last notification? I could easily dodge the first one because the height is 0, but the second one looks like a valid height and I don't find an answer on how to solve this.
Here is what I've so far:

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

我的日志输出是:

键盘出现
高度:0.0
键盘出现
高度:216.0
键盘出现
高度:258.0
键盘消失

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

那么有什么办法只能处理第三个通知而忽略"前两个通知吗?

So is there any way to only handle the third notification and "ignore" the first two?

推荐答案

将所有波纹管字段设置为NO可以解决此问题.

Set all bellow fields to NO can resolve this problem.

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

这篇关于UIKeyboardWillShowNotification被调用了三次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆