【问题标题】:Keyboard height change observer swift键盘高度变化观察者 swift
【发布时间】:2016-12-03 17:55:51
【问题描述】:

如何在 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()
}

如何调整视图?

【问题讨论】:

  • 观察通知UIKeyboardWillChangeFrameNotification
  • 感谢您的重播。我在 UIKeyboardWillChangeFrameNotification 中更改了它。但它不会工作。发送一些示例代码。

标签: swift keyboard


【解决方案1】:

像这样使用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
【解决方案2】:

斯威夫特 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...
    }
}

【讨论】:

    【解决方案3】:

    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)
            })
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 2011-04-07
      相关资源
      最近更新 更多