键盘高度变化观察者

时间:2016-05-22 19:20:01

标签: ios swift uikeyboard

如何使用Swift检测iOS中的键盘高度变化或键盘更改。

我可以为我的应用添加一个观察者来检测键盘显示与否:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentView.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentView.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)

我根据那个改变我的按钮位置:

func keyboardWillShow(notification: NSNotification) {
        animateTextFieldWithKeyboard(notification)
}

func keyboardWillHide(notification: NSNotification) {
        animateTextFieldWithKeyboard(notification)
}

func animateTextFieldWithKeyboard(notification: NSNotification) {


let userInfo = notification.userInfo!

let keyboardSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double
let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as! UInt

// baseContraint is your Auto Layout constraint that pins the
// text view to the bottom of the superview.

if notification.name == UIKeyboardWillShowNotification {
    if (BottomConstraint.constant == 0) {
        BottomConstraint.constant += keyboardSize.height
    }

    // move up
}
else {
    BottomConstraint.constant = 0
    // move down
}

view.setNeedsUpdateConstraints()

let options = UIViewAnimationOptions(rawValue: curve << 16)
UIView.animateWithDuration(duration, delay: 0, options: options,
    animations: {
        self.view.layoutIfNeeded()
    },
    completion: nil
)

}

正如您在屏幕截图中看到的,一切正常:

enter image description here

但是当我将键盘类型更改为表情符号时会出现问题。它隐藏了我的textField和我的Button,所以我想根据键盘的新高度更改按钮和TextFiend的位置

enter image description here

2 个答案:

答案 0 :(得分:1)

如果您在Xcode文档中搜索UIKeyboardWillShowNotification,则会转到U​​IWindow上的部分,该部分最后会有一个通知表。

我建议您尝试UIKeyboardWillChangeFrameNotification

找到答案的时间:大约30秒。

答案 1 :(得分:1)

我使用此功能并触发所有通知。

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

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)

}    

@objc func keyboardWillShow(notification: NSNotification) {
    let keyboardSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue

        if keyboard == false{
            keyboard = true
            lastKeyboardHeight = keyboardSize.height
            chatDetailView.frame.origin.y = chatDetailView.frame.origin.y-(keyboardSize.height-bottomMenu.frame.height)
        }
    }

    @objc func keyboardWillChange(notification: NSNotification) {
        let keyboardSize1 = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue

        if keyboard == true && lastKeyboardHeight != keyboardSize1.height {
            if lastKeyboardHeight < keyboardSize1.height{
                let keyboardDifference: CGFloat = keyboardSize1.height-lastKeyboardHeight
                chatDetailView.frame.origin.y -= keyboardDifference

            } else {
                let keyboardDifference: CGFloat = lastKeyboardHeight-keyboardSize1.height
                chatDetailView.frame.origin.y += keyboardDifference
            }
            lastKeyboardHeight = keyboardSize1.height
        }
    }

    @objc func keyboardWillHide(notification: NSNotification) {
        if keyboard == true {
            keyboard = false
            chatDetailView.frame.origin.y = chatDetailView.frame.origin.y+(lastKeyboardHeight-bottomMenu.frame.height)
        }
    }