Ios 键盘显示额外的空间
原始屏幕截图:Ios 键盘显示额外的空间,ios,swift,uitableview,keyboard,constraints,Ios,Swift,Uitableview,Keyboard,Constraints,原始屏幕截图: func addKeyboardObservers() { NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil) NotificationCenter.default.addObserver(self, selecto
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()
}
1.当键盘不可见时:
这很好
2。问题是当键盘出现时,上述图像从文本视图和键盘的原始位置移到上方,如下所示:
以下是我试图做的:
代码:
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)
// }
}
这是我的主要情节提要屏幕截图:
有人能给我解释一下如何解决这个问题吗?我已经尝试过解决这个问题,但还没有结果
任何帮助都将不胜感激
提前感谢。使用以下属性在键盘上隐藏建议拼写视图,希望对您有所帮助
textField.autocorrectionType = .no
你会经常在新手机上看到这种“间隙”,因为它们使用“安全区域插入”,要减小对象和键盘之间的间隙,你需要做两件事
var bottomAnchor: NSLayoutConstraint? // Creating variable for bottom constraint
// Setting Constraints
bottomAnchor = tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
bottomAnchor.isActive = true
// Modifying Constraint later (When Keyboard is Shown)
bottomAnchor.constant = keyboardFrame.height - safeArea
如果要修改UIEDGEINSET,则只需将底部值减小到安全区域即可
// When Keyboard is Shown
// E.g.
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardFrame.height - safeArea, right: 0)
为什么要尝试手动更新视图?为此使用IQKeyboardManager什么是
self.bottomConstraint
?@AtulParmar i IQKeyboardManager使用,但相同result@mag_zbc这是我的错误。您想从键盘隐藏建议拼写视图吗?如果您使用了IQKeyboardManager,请禁用此视图并通过普通键盘