【问题标题】:Keyboard Show Extra Space Swift键盘显示额外空间 Swift
【发布时间】: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


【解决方案1】:

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

textField.autocorrectionType = .no

【讨论】:

  • 如果您使用过 IQKeyboardManager,请禁用此视图并使用普通键盘
【解决方案2】:

您会在新手机上经常看到这种“间隙”,因为它们使用“safeAreaInsets”,并且要减小对象和键盘之间的间隙,您需要做两件事。

  1. 获取键盘高度。
  2. 获取视图的安全区域,您可以将其放入这样的变量中。
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)

【讨论】: