【问题标题】:How to implement "move view with keyboard" function into textfield如何在文本字段中实现“使用键盘移动视图”功能
【发布时间】:2018-12-29 07:53:20
【问题描述】:

我想将该功能实现到我的文本字段的功能中。我尝试了不同的方法,但我无法成功。
代码如下:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0 {
            self.view.frame.origin.y -= keyboardSize.height
        }
    }
}

【问题讨论】:

标签: ios swift swift4 nsnotifications


【解决方案1】:

您正在使用密钥UIResponder.keyboardFrameBeginUserInfoKey,这导致您得到错误的高度。

改用这个键:

UIResponder.keyboardFrameEndUserInfoKey

【讨论】:

    猜你喜欢
    • 2018-11-03
    • 2016-07-24
    • 2018-12-26
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    相关资源
    最近更新 更多