当前位置: 首页 > 知识库问答 >
问题:

键盘覆盖视图底部的文本字段

何楷
2023-03-14

我已经搜索过了

此处:仅当键盘覆盖输入字段时,才向上移动视图

此处:键盘快速显示时移动文本字段

这里:当键盘存在时,如何使UITextField向上移动?

这里:https://developer.apple.com/library/content/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

不幸的是,我能找到的所有链接和似乎所有其他地方都没有给出我正在寻找的好的干净html" target="_blank">解决方案。它们要么在Obj-c代码中过时,要么在Xcode 9和Swift 4的当前迭代中不起作用。

如何管理覆盖视图底部文本字段的键盘?我希望屏幕的视图仅在键盘覆盖文本字段视图时才移动,而不使用滚动视图,并且能够对此进行动画处理以使其看起来很漂亮,而不是让它只是捕捉,最重要的是我不想使用外部库。

Apple的CoreAnimation库非常棒,但是所有的示例代码都已经过时了,而且都是在objective c中,而objective c现在已经被弃用了(我不敢相信Apple没有更新他们的文档)。

如果有人能给我指出正确的方向,让我找到最新的代码,或者我所缺少的一个专门解决这个问题的苹果库,我将不胜感激。

共有3个答案

艾泽语
2023-03-14

在多个文本字段的情况下

我只实现了底部的文本字段(不使用通知观察者)。

如果您使用的是scrollView,此代码可能会有所帮助(scrollViewddScroll是可选的)

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    scrollView.contentSize = CGSize(width: self.scrollView.frame.size.width, height: (scrollView.frame.size.height + 300))// To be more specific, I have used multiple textfields so wanted to scroll to the end.So have given the constant 300.
}

func textFieldDidBeginEditing(_ textField:UITextField) {
    self.scrollView.setContentOffset(textField.frame.origin, animated: true)
}

如果要根据视图设置textfields位置,请尝试以下操作:

func textFieldDidBeginEditing(_ textField:UITextField){
    textField.frame.origin.y = textField.frame.origin.y - 150 //(If have not used contentsizing the scroll view then exclude this line)default origin takes the texfield to the top of the view.So to set lower textfields to proper position have used the constant 150.
    self.scrollView.setContentOffset(textField.frame.origin, animated: true)
}

专门针对底部的文本字段执行。检查其标记值文本字段。textFieldDidBeginEditing中的标记

func textFieldDidBeginEditing(_ textField:UITextField){
    if textField.tag = 4 { //tag value of the textfield which are at the bottom
        self.scrollView.setContentOffset(textField.frame.origin, animated: true)
    }
}

如果您在tableView中实现了文本字段,请使用下面解释的通知观察者。

如果一个tableView中有多个文本字段,最好使用通知观察者

override func viewDidAppear(_ animated: Bool) {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
}

@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardHeight = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0)
    }
}

@objc func keyboardWillHide(notification: NSNotification) {
    UIView.animate(withDuration: 0.2, animations: {
        // For some reason adding inset in keyboardWillShow is animated by itself but removing is not, that's why we have to use animateWithDuration here
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
    })
}

deinit {
    print("denit")
    NotificationCenter.default.removeObserver(self)
}
鱼浩荡
2023-03-14

代码将起作用,如果其框架与键盘相交,则使您的text Field动画到键盘上方,并动画回到键盘隐藏上的原始位置。

@IBOutlet weak var textField: UITextField!

  var offsetY:CGFloat = 0

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardFrameChangeNotification(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
  }

  func keyboardFrameChangeNotification(notification: Notification) {
    if let userInfo = notification.userInfo {
      let endFrame = userInfo[UIKeyboardFrameEndUserInfoKey] as? CGRect
      let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double ?? 0
      let animationCurveRawValue = (userInfo[UIKeyboardAnimationCurveUserInfoKey] as? Int) ?? Int(UIViewAnimationOptions.curveEaseInOut.rawValue)
      let animationCurve = UIViewAnimationOptions(rawValue: UInt(animationCurveRawValue))
      if let _ = endFrame, endFrame!.intersects(self.textField.frame) {
        self.offsetY = self.textField.frame.maxY - endFrame!.minY
        UIView.animate(withDuration: animationDuration, delay: TimeInterval(0), options: animationCurve, animations: {
          self.textField.frame.origin.y = self.textField.frame.origin.y - self.offsetY
        }, completion: nil)
      } else {
        if self.offsetY != 0 {
          UIView.animate(withDuration: animationDuration, delay: TimeInterval(0), options: animationCurve, animations: {
            self.textField.frame.origin.y = self.textField.frame.origin.y + self.offsetY
            self.offsetY = 0
          }, completion: nil)
        }
      }
    }
  }
郎子平
2023-03-14

您可以使用IQKeyboardManagerSwift轻松快速地解决问题。

在您的pod文件中使用以下pod,以支持Swift 4。

pod 'IQKeyboardManagerSwift', '5.0.0'

这里是实现IQKeyboardManagerSwift的链接。

https://github.com/hackiftekhar/IQKeyboardManager

谢谢

 类似资料:
  • 清单文件Android:Windowsoftinputmode=“AdjustPanAdjustReadstresizeStateVisible”我想在键盘显示和标题栏不熄灭时在键盘顶部显示serachview。有什么办法吗?XMl页面

  • Android:show软键盘在点击布局底部实现的特定Edittext后自动覆盖Edittext字段的一半。当请求焦点在EditText软键盘上打开时,它将向上移动布局,但会覆盖EditText字段的一半。

  • 我的列表视图底部有一个编辑文本。当我点击编辑文本时,软键盘如预期的那样弹出,同时listview向上滑动,将编辑文本放置在键盘上方。然而,一旦我开始输入,ListView就会滑回来,EditText现在位于软键盘后面,挡住了输入的视图。我仍然可以打字,但是编辑文本被软键盘覆盖了。 如何使编辑文本在键盘上方可见? PS。我正在使用adjustPan 编辑:这是一个与我面临的问题相同的人的链接,但我看

  • 问题内容: 我正在尝试为iPhone建立输入屏幕。屏幕上有许多输入字段。它们大多数位于屏幕顶部,但两个字段位于底部。当用户尝试在屏幕底部编辑文本时,键盘将弹出并覆盖屏幕。我找到了一种在发生这种情况时将屏幕向上移动的简单解决方案,但是结果是屏幕 始终 向上移动,并且当用户尝试编辑屏幕时,屏幕顶部的字段超出了范围。 有没有一种方法 只有 在编辑底部字段 时才 移动屏幕? 我使用了在这里找到的这段代码:

  • 我正在尝试为iPhone构建一个输入屏幕。屏幕有一个进审量字段。它们大多在屏幕顶部,但底部有两个字段。当用户尝试编辑屏幕底部的文本时,键盘会弹出,它会覆盖屏幕。当这种情况发生时,我找到了一个简单的解决方案来向上移动屏幕,但结果是屏幕总是向上移动,当用户尝试编辑那些时,屏幕顶部的字段移动到够不着的地方。 是否有办法使屏幕仅在编辑底部字段时移动? 我使用了我在这里找到的代码:

  • 问题内容: 我想通过拦截文档对象(而不是accesskey属性)的keypress事件处理程序,来为Web应用程序中的几个页面添加对键盘快捷键的支持。 问题是,每个浏览器都有自己的组合键,因此不可能拿出一套键盘组合可在所有网络浏览器的工作,但一致的。(例如,它会是愚蠢的,如果在保存快捷为++ ,而一个要删除的是+ 。) 因此,我认为在我的几个页面中完全覆盖浏览器快捷方式会更简单。 撇开所有不利因素