gpt4 book ai didi

ios - 键盘显示额外空间 Swift

转载 作者:行者123 更新时间:2023-11-28 07:30:44 24 4
gpt4 key购买 nike

原始屏幕截图:

1.当键盘不可见时:

screen Shot

这很好。

2. 问题是当键盘出现时,上面的图像从 textView 和键盘的原始位置向上移动,如下所示:

Screen Shot

这是我正在尝试做的事情:

Screen Shot

代码:

  func addKeyboardObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}

// MARK: - remove Keyboard Observers
func removeKeyboardObservers() {
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}

// MARK: - keyboard show
@objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height - bottomLayoutGuide.length
UIView.animate(withDuration: 0.25, animations: {() -> Void in
self.bottomConstraint.constant = keyboardHeight
self.view.layoutIfNeeded()
}, completion: nil)
}
}

// MARK: - keyboard hide
@objc func keyboardWillHide(_ notification: Notification) {
view.layoutIfNeeded()
}

当我使用这段代码时,给我看下面给出的空白屏幕截图:

Screen Shot

当我评论下面这些行的屏幕截图时:

// MARK: - keyboard show

@objc func keyboardWillShow(_ notification: Notification) {
// if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
// let keyboardRectangle = keyboardFrame.cgRectValue
// let keyboardHeight = keyboardRectangle.height - bottomLayoutGuide.length
// UIView.animate(withDuration: 0.25, animations: {() -> Void in
// self.bottomConstraint.constant = keyboardHeight
// self.view.layoutIfNeeded()
// }, completion: nil)
// }
}

Screen Shot

这是我的主 Storyboard屏幕截图:

Screen Shot

有人可以向我解释如何解决这个问题吗,我已经尝试解决这个问题但还没有结果。

如有任何帮助,我们将不胜感激。

提前致谢。

最佳答案

使用以下属性从键盘隐藏建议拼写 View ,希望对您有所帮助

textField.autocorrectionType = .no

enter image description here

关于ios - 键盘显示额外空间 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54806879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com