在iOS 11上未调用UIKeyboardWillChangeFrame(转换视图控制器时)

时间:2017-09-22 08:59:08

标签: ios swift uikeyboard

问题

我在iOS 11上有很多案例(在iOS 10及更低版本中没有出现),其中.UIKeyboardWillChangeFrame通知未被触发 - 特别是在两个视图控制器都有UITextfield的视图控制器之间转换时设置为fristResponder。

由于我的UI需要在键盘上方显示动画以响应键盘显示,因此接收此通知至关重要。

在iOS 10及以下版本中,我在两个视图控制器上都会收到通知(显示VC A时以及推送VC B时)。但是,在iOS 11上,按下VC B时通知不会触发。就像键盘保持原位一样。

有谁知道这是什么原因?

详细信息

两个视图控制器都从基本视图控制器继承,具有以下实现:

class BaseViewController {
override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeFrame), name: .UIKeyboardWillChangeFrame, object: nil)

}

// Update layout when the keyboard is shown or hidden
@objc func keyboardWillChangeFrame(notification : Notification) {

    // Check if got info
    if (notification.userInfo == nil) {
        return;
    }

    // Get resize properties
    let dict = notification.userInfo!
    let rect = self.view.convert((((dict[UIKeyboardFrameEndUserInfoKey as NSObject] as Any) as AnyObject).cgRectValue)!, from: nil)
    let size = self.view.bounds.size.height - rect.origin.y
    let duration = ((dict[UIKeyboardAnimationDurationUserInfoKey] as Any) as AnyObject).doubleValue
    let curve = UIViewAnimationCurve.init(rawValue: (((dict[UIKeyboardAnimationCurveUserInfoKey] as Any) as AnyObject).intValue)!)
    self.keyboardOffset = max(0, size)

    // Set animation options
    var options : UIViewAnimationOptions
    switch (curve!) {
    case .easeInOut:
        options = UIViewAnimationOptions()
    case .easeIn:
        options = UIViewAnimationOptions.curveEaseIn
    case .easeOut:
        options = UIViewAnimationOptions.curveEaseOut
    case .linear:
        options = UIViewAnimationOptions.curveLinear
    }

    // Animate the change
    UIView.animate(withDuration: duration!, delay: 0, options: options, animations: { () -> Void in

        // Relayout
        self.relayout()

    }, completion: nil)

}
}

子类的示例:

class ViewControllerA: BaseViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        passwordField.becomeFirstResponder()
    }

    func relayout() {
    // ... do animations to show stuff above keyboard.
    }

}

请注意,转换到VC B时也不会触发.keyboardWillShow.keybaordWillHide

2 个答案:

答案 0 :(得分:0)

您需要在第一个视图控制器的self.view.endEditing(force:Bool)方法中添加viewWillDisappear

通常,您可以在更改屏幕时调用endEditing方法。

答案 1 :(得分:0)

我通过将键盘高度保存在VC A中的类变量中然后将其传递给VC B来解决这个问题。 这并不理想,因为我希望我的VC能够独立于这些细节。