键盘显示额外 Space Swift
Keyboard Show Extra Space Swift
原始屏幕截图:
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()
}
当我使用此代码时,显示空白 space 屏幕截图如下:
当我评论下面这些行的屏幕截图时:
// 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
您会在新手机上经常看到这种“间隙”,因为它们使用 'safeAreaInsets' 并且要减小您的对象和键盘之间的间隙,您需要做两件事。
- 获取键盘高度。
- 得到你view的safe area,你可以像这样把它放到一个变量中。
let safeArea = view.safeAreaInsets.bottom // This will result CGFloat
不只是减少两个值,你会得到完美的输出。
如果您正在使用约束,那么您可以更改底部锚点的常量。
我将在此示例中使用 TableView,但您可以将其应用于任何对象。
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
如果您正在修改 UIEdgeInsets,那么您只需将底部值减小到安全区域即可。
// When Keyboard is Shown
// E.g.
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardFrame.height - safeArea, right: 0)
原始屏幕截图:
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()
}
当我使用此代码时,显示空白 space 屏幕截图如下:
当我评论下面这些行的屏幕截图时:
// 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
您会在新手机上经常看到这种“间隙”,因为它们使用 'safeAreaInsets' 并且要减小您的对象和键盘之间的间隙,您需要做两件事。
- 获取键盘高度。
- 得到你view的safe area,你可以像这样把它放到一个变量中。
let safeArea = view.safeAreaInsets.bottom // This will result CGFloat
不只是减少两个值,你会得到完美的输出。
如果您正在使用约束,那么您可以更改底部锚点的常量。
我将在此示例中使用 TableView,但您可以将其应用于任何对象。
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
如果您正在修改 UIEdgeInsets,那么您只需将底部值减小到安全区域即可。
// When Keyboard is Shown
// E.g.
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardFrame.height - safeArea, right: 0)