ios - 如何将 "move view with keyboard"函数实现到文本字段中

标签 ios swift swift4 nsnotifications

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

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

最佳答案

您使用的键 UIResponder.keyboardFrameBeginUserInfoKey 导致您得到错误的高度。

改用这个键:

UIResponder.keyboardFrameEndUserInfoKey

关于ios - 如何将 "move view with keyboard"函数实现到文本字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53967740/

相关文章:

iOS:读取PDF文件的内容

swift - 使用 Dropbox SDK 时无法调用 AppDelegate openURL( )

ios - 在 Swift 中将数字四舍五入到小数点后两位

ios - 尝试在 Swift 4、IOS 11 中使用 Filter 时出现错误消息

ios - 无法到达 Firestore 后端

ios - UICollectionView header 兼容 View

ios - 如何从 iPhone 应用程序中删除客户端证书?

ios - 我无法导入我需要的东西,以使用 MaterialContainerScheme

ios - transitionfromview 和 addsubview 的区别

dictionary - Swift:如何在字符串表达式中打印出字典键的值?