Swift键盘高度变化观察器

6
如何检测iOS Swift中键盘高度的更改或键盘更改? 请参见我的以下代码,它显示文本键盘和表情符号键盘中非常小的行:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardHideNShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardHideNShow(_:)), name: UIKeyboardWillHideNotification, object: nil)
var didDisplayedKeyboard:Bool = false

func keyboardHideNShow(notification:NSNotification) {

var movement: CGFloat = 0.0
let userInfo:NSDictionary = notification.userInfo!
let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.CGRectValue()

let movementDuration:NSTimeInterval = 0.3
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration )

if notification.name == UIKeyboardWillShowNotification {
    // Do the operation only if its hide n show or show n hide
    // When the keyboard switches from text to emoji, it wont hide the previous keyboard. will just replace
    //      In that case we need to avoid the keyboard movement
    if didDisplayedKeyboard == false {
        movement = -keyboardRectangle.height
        didDisplayedKeyboard = true
        print("m\(movement)")
        self.view.frame = CGRectOffset(self.view.frame, 0,  movement)
    }

} else if notification.name == UIKeyboardWillHideNotification {

    movement =  keyboardRectangle.height
    didDisplayedKeyboard = false
    self.view.frame = CGRectOffset(self.view.frame, 0,  movement)
}
UIView.commitAnimations()
}

如何调整我的视图?


2
观察通知 UIKeyboardWillChangeFrameNotification - zylenv
谢谢您的回复。我已经在UIKeyboardWillChangeFrameNotification中进行了更改,但它仍然无法正常工作。请发送一些示例代码。 - Mathi
3个回答

6

可以像这样使用UIKeyboardWillChangeFrameNotification通知:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillChangeFrame(_:)), name: UIKeyboardWillChangeFrameNotification, object: nil)

并接收更改为:
func keyboardWillChangeFrame(notification: NSNotification) {
    if let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
        let keyboardHeight = keyboardFrame.size.height
        print("keyboard height: \(keyboardHeight)")
        //do the chnages according ot this height
    }
}

当键盘出现、切换到表情符号或显示/隐藏预测时,该通知会提供键盘框架矩形。


你的问题解决了吗?@mathi - D4ttatraya

3

Swift 4:

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

然后,您可以按如下方式处理键盘框架:

@objc func keyboardWillChangeFrame(_ notification: Notification) {
    if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
        let keyboardHeight = keyboardFrame.cgRectValue.size.height
        // Do something...
    }
}

2

Swift 5:

NotificationCenter.default.addObserver(self, selector: #selector(_KeyboardHeightChanged(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)

然后像这样更改您的视图底部约束:

@objc private func _KeyboardHeightChanged(_ notification: Notification){
    if let frame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue{
        UIView.animate(withDuration: 0.5, animations: {
            self.SampleViewBottomConstaint.constant == 0 ? (self.SampleViewBottomConstaint.constant = frame.cgRectValue.height) : (self.SampleViewBottomConstaint.constant = 0)
        })

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接