在附带硬件键盘的ios 9中,键盘将在单击文本字段时隐藏触发器

时间:2015-09-26 00:17:43

标签: ios swift

注意到在ios 9中,单击附加了硬件键盘的文本框时,我的keyboardWillHide事件被错误触发。在ios9中有什么变化吗?我应该以某种方式尝试检测键盘未显示,还是应该尝试找出为什么要调用keyboardWillHide通知并阻止它?

import UIKit

class BaseTextFieldViewController: UIViewController {

    // MARK: Keyboard handling

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
    }

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

    func keyboardWillShow(sender: NSNotification) {
        if let userInfo = sender.userInfo {
            if let keyboardHeight = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue.size.height {
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.view.center = CGPointMake(self.view.center.x, self.view.center.y - keyboardHeight)
                })
            }
        }
    }

    func keyboardWillHide(sender: NSNotification) {
        if let userInfo = sender.userInfo {
            if let keyboardHeight = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue.size.height {
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.view.center = CGPointMake(self.view.center.x, self.view.center.y + keyboardHeight)
                })
            }
        }
    }

}

0 个答案:

没有答案