键盘覆盖了我视图底部的文本字段 [英] Keyboard covers text fields at the bottom of my view

查看:94
本文介绍了键盘覆盖了我视图底部的文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了

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

此处:如何在出现键盘时使UITextField向上移动?

和此处: https://developer.apple.com/library/content/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

不幸的是,所有链接以及我能找到的其他地方似乎都无法提供理想的干净解决方案.它们要么已经过时了Obj-c代码,要么在使用 Swift 4 Xcode 9 的当前迭代中不起作用.

Unfortunately, all of the links and seemingly everywhere else I can find do not give a good clean solution that I am looking for. They are either outdated with Obj-c code, or plain do not work in the current iteration of Xcode 9 with Swift 4.

如何管理覆盖视图底部文本字段的键盘?我希望仅当屏幕键盘覆盖文本字段视图时才移动屏幕视图,而不使用滚动条视图,并具有将其动画化的功能,使其看起来更漂亮,而不是仅仅让它捕捉,而且最重要的是,我不想使用外部库.

How do I manage a keyboard that is covering text fields at the bottom of my view? I want the screen's view to move only when they keyboard is covering the text field view, without using a scroll view, with the ability to animate this to make it look pretty rather than have it just snap, and most importantly I do not want to use an outside library.

Apple的CoreAnimation库非常强大,但所有示例代码都已过时,并且在目标c中已弃用(我不相信Apple不会更新其文档).

CoreAnimation libraries from Apple are great and all, but all of the sample code is outdated and in objective c which is deprecated at this point (I cannot believe that Apple isn't updating their documentation).

如果有人可以向我指出更新的最新代码或Apple的库的正确方向,那么我很想念这个问题,将不胜感激.

If someone could point me in the right direction to updated and current code or a library from Apple I am missing that will specifically address this issue, it would be much appreciated.

推荐答案

此代码将起作用,如果textField的帧与键盘的帧相交,则使textField动画到键盘上方,并动画回到键盘隐藏的原始位置.

This code will work, making your textField animating to above keyboard if its frame intersects with that of keyboard and animating back to original position on keyboard hide.

@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)
        }
      }
    }
  }

这篇关于键盘覆盖了我视图底部的文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆