ios - 关闭键盘的平滑过渡

标签 ios swift keyboard

我正在开发一个聊天应用程序,我试图让用户以交互方式关闭键盘。不幸的是,它没有像我预期的那样工作:

image

(这是我开始拉下键盘的时候。我希望我的 TextView 无论如何都留在键盘的顶部。这是我尝试过的:

viewDidLoad中:

textView.keyboardDismissMode = .interactive//I have tried the same thing with the table view as well, but it produces the same result.

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

在同一个类(class):

func keyboardWillShow(_ aNotification: NSNotification) {
    let info = aNotification.userInfo!
    let keyboardFrame = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue()
    UIView.animate(withDuration: 0.25) { () -> Void in
        self.messageBottomConstraint.constant = keyboardFrame.height
        self.view.layoutIfNeeded()

    }
}

func keyboardWillHide(_ aNotification: NSNotification) {
    UIView.animate(withDuration: 0.25) { () -> Void in
        self.messageBottomConstraint.constant = 0
        self.view.layoutIfNeeded()

    }
}

如何在用户关闭时将 TextView 保持在键盘顶部?谢谢!

最佳答案

您应该覆盖 UIResponder 上的 inputAccessoryViewinputAccessoryViewController 属性(UIViewController 继承自)。这将允许您提供一个 View / View Controller ,当它出现时自动固定到键盘的顶部。

This blog post举例说明如何执行此操作。

关于ios - 关闭键盘的平滑过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38624638/

相关文章:

ios - TabController 上的 ContainerView 创建 Phantom Tab

iphone - NSMutableArray removeObjectAtIndex : Behaviour

swift 字典 : Can't completely remove entry

ios - 在 swift IOS 中使代码更有条理

ios - 通过用户 ID 或某个唯一 ID :Issue 获取 PFInstallation

ios - Opencv 可以在c/cpp 中开发- 在IOS 中运行吗?

swift - 为什么 Storyboard弹出框出现在导航 Controller 内的后续 View 上

ios - 值类型和 init 的问题

ios - 如何在 Beta 3 中设备旋转时正确处理自定义键盘

swift - 导航后键盘消失