ios - 旋转后接收两个不同高度的键盘

标签 ios swift swift2 nsnotificationcenter

我有一个 View ,它在键盘出现时调整约束的大小。因此,当键盘出现和消失时,我会收到通知。

当键盘已经显示并且我旋转屏幕时会发生上述行为。然后发生下一个 Action :

  1. 已调用 UIKeyboardWillHideNotification
  2. 调用了 UIKeyboardWillShowNotification(使用键盘的旧高度)
  3. 调用了 UIKeyboardWillShowNotification(使用新的键盘高度)

因此,updateView 函数首先接收一个高度,然后接收一个不同的高度。这会导致 View 奇怪地移动两倍的值。

override func viewDidLoad() {

    super.viewDidLoad()

    // Creates notification when keyboard appears and disappears
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)

}

deinit {

    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)

}

func keyboardWillShow(notification: NSNotification) {

    self.adjustingHeight(true, notification: notification)

}

func keyboardWillHide(notification: NSNotification) {

    self.adjustingHeight(false, notification: notification)

}

private func adjustingHeight(show: Bool, notification: NSNotification) {

    // Gets notification information in an dictionary
    var userInfo = notification.userInfo!
    // From information dictionary gets keyboard’s size
    let keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
    // Gets the time required for keyboard pop up animation
    let animationDurarion = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSTimeInterval
    // Animation moving constraint at same speed of moving keyboard & change bottom constraint accordingly.
    if show {
        self.bottomConstraint.constant = (CGRectGetHeight(keyboardFrame) + self.bottomConstraintConstantDefault / 2)
    } else {
        self.bottomConstraint.constant = self.bottomConstraintConstantDefault
    }
    UIView.animateWithDuration(animationDurarion) {
        self.view.layoutIfNeeded()
    }

    self.hideLogoIfSmall()

}

最佳答案

终于找到了解决办法。当我得到 keyboardFrame 时,我使用的是 UIKeyboardFrameBeginUserInfoKey,它会在动画开始之前返回键盘的框架。正确的做法是使用 UIKeyboardFrameEndUserInfoKey,它会在动画完成后返回键盘的框架。

let keyboardFrame: CGRect = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()

关于ios - 旋转后接收两个不同高度的键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35223224/

相关文章:

ios - 如何从 NSDate 对象中检索午夜后的小时数?

android - 我应该将 Ionic Platforms 文件夹放入源代码管理吗?

arrays - 获取 JSON 数据以从数组填充 TableView

ios - fatal error : unexpectedly found nil while unwrapping an Optional value Swift Using Alamofire in UICollectionView

ios - Swift - 如何强制用户在 UITextField 中只导入整数?

ios - 在 UILabel 上使用 NSMutableAttributedString 时表情符号丢失

ios - 是否可以从子类中推送 View Controller ?

ios - prepare传入的对象是单例吗?有什么区别?

ios - JSQMessagesViewController changedBubbleImage 不工作

arrays - 未解析的标识符错误 Swift