当用户单击uiwebview内部的文本字段时,屏幕不向上移动



我有一个iPhone应用程序(swift 3),其中内容会从CMS中动态加载登录名。该表格包含用户需要输入和提交的字段,这没什么棘手的。

不幸的是,当用户单击登录字段时,会出现键盘,并且它隐藏了包含表单的UIWebView。

我如何能够解决这个问题?
请检查屏幕截图,单击隐藏我的uiwebview内容的表格

尝试此代码,这将适用于 swift 3 。在这种情况下,我给了4个Textfields

import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
  @IBOutlet weak var textField4: UITextField!
  @IBOutlet weak var textField3: UITextField!
  @IBOutlet weak var textField2: UITextField!
  @IBOutlet weak var textField1: UITextField!
  override func viewWillDisappear(_ animated: Bool)
  {
    view.endEditing(true)
  }
  override func viewDidLoad() {
      super.viewDidLoad()
      self.textField1.delegate = self
      self.textField2.delegate = self
      self.textField3.delegate = self
      self.textField4.delegate = self
      self.textField1.tag = 0
      self.textField2.tag = 1
      self.textField3.tag = 2
      self.textField4.tag = 3
     }
  func textFieldShouldReturn(_ textField: UITextField) -> Bool {
      textField.resignFirstResponder()
      return true
  }
  func textFieldDidBeginEditing(_ textField: UITextField) {
      switch textField.tag {
      case 0:
          moveTextField(textField: textField, moveDistance: -120, up: true)
      case 1:
          moveTextField(textField: textField, moveDistance: -140, up: true)
      case 2:
          moveTextField(textField: textField, moveDistance: -160, up: true)
      case 3:
          moveTextField(textField: textField, moveDistance: -180, up: true)
      default:
          print("Do Nothing...!")
      }
  }
  func textFieldDidEndEditing(_ textField: UITextField) {
      switch textField.tag {
      case 0:
          moveTextField(textField: textField, moveDistance: -120, up: false)
      case 1:
          moveTextField(textField: textField, moveDistance: -140, up: false)
      case 2:
          moveTextField(textField: textField, moveDistance: -160, up: false)
      case 3:
          moveTextField(textField: textField, moveDistance: -180, up: false)
      default:
          print("Do Nothing...!")
      }
  }

  func moveTextField(textField: UITextField, moveDistance: Int, up: Bool)
  {
      let moveDuration = 0.3
      let movement: CGFloat = CGFloat(up ? moveDistance : -moveDistance)
      UIView.beginAnimations("animateTextField", context: nil)
      UIView.setAnimationBeginsFromCurrentState(true)
      UIView.setAnimationDuration(moveDuration)
      self.view.frame = (self.view.frame).offsetBy(dx: 0, dy: movement)
      UIView.commitAnimations()
   }
}
override func viewDidLoad() {
        super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            self.view.frame.origin.y = -keyboardSize.height/2
    }
}
func keyboardWillHide(notification: NSNotification) {
    if let _ = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            self.view.frame.origin.y = 0
    }
}

相关内容

  • 没有找到相关文章

最新更新