快速显示键盘问题和预测文本

时间:2018-06-08 11:03:58

标签: ios swift xcode

我一直在搜索并且没有找到关于向用户显示我的键盘的可靠答案。

如果用户打开或关闭了预测文本(键盘上方出现灰色字符框),则问题在于此。

如果用户打开或关闭预测文本,我会禁用预测框来解释我的键盘被推高的奇怪行为。 (如果预测文本打开,视图看起来很好。如果它的关闭有大约50个点的差距因此在键盘中会改变func我加50点)

autocorrectionType引用如何控制预测文本框以及textView中的拼写检查?必须有一种方法来禁用框,仍然允许拼写检查?如果你们没有更好的方法来解释这两个选项?

这是我的代码。非常感谢任何帮助!

override func viewDidLoad() {

    captionTextView.autocorrectionType = .no // disables predicitve text from coming up

    captionTextView.spellCheckingType = .yes // Have tried but doesn't make a difference

    NotificationCenter.default.addObserver(self, selector: #selector(IntegratedCVC.keyboardWillChange(sender:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

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

    NotificationCenter.default.addObserver(self, selector: #selector(IntegratedCVC.keyboardWillChange(sender:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}


@objc func keyboardWillChange(sender: NSNotification) {
    guard let keyboardRect = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue else {
        return
    }
    if sender.name == NSNotification.Name.UIKeyboardWillShow || sender.name == NSNotification.Name.UIKeyboardWillChangeFrame {

            self.view.frame.origin.y = -keyboardRect.height + 50 // i add 50 to account for strange behavior with my view getting pushed up 
            self.view.layoutIfNeeded()


    }
}

@objc func keyboardWillHide(sender: NSNotification) {

    if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y != 0 {
            self.view.frame.origin.y = 0
        }
    }

}

0 个答案:

没有答案