用户单击UIWebView

时间:2017-09-08 07:24:37

标签: ios swift3 uiwebview keyboard

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

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

我如何解决这个问题? Please Check Screenshot, click the form hiding my UIWebView Contents

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

试试此代码,这适用于 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()
   }
}