【发布时间】:2019-07-15 08:31:31
【问题描述】:
原始屏幕截图:
1.当键盘不可见时:
这很好。
2.问题是当键盘出现时,上面的图像从textView和键盘的原始位置向上移动,如下所示:
这是我想要做的:
代码:
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()
}
当我使用此代码时,请显示下面给出的空白屏幕截图:
当我在下面评论这些行屏幕截图时:
// 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)
// }
}
这是我的主要故事板屏幕截图:
谁能告诉我如何解决这个问题,我已经尝试解决这个问题但还没有结果。
任何帮助将不胜感激。
提前致谢。
【问题讨论】:
-
为什么要手动更新视图?为此使用 IQKeyboardManager
-
什么是
self.bottomConstraint? -
@AtulParmar 我使用了 IQKeyboardManager 但结果相同
-
@mag_zbc 这是我的错误
-
您想从键盘隐藏建议拼写视图吗?
标签: ios swift uitableview keyboard constraints