简体   繁体   中英

Screen not moving up after keyboard appears when user clicks on text field inside UIWebView

I have an iPhone application (Swift 3) where the content dynamically loads a Login from a CMS. The form contains fields where a user needs to enter and submit, nothing tricky.

Unfortunately, when the user clicks on the Login Field, the keyboard appears and it is hiding the UIWebView that is containing the form.

How am I able to resolve this?
Please Check Screenshot, click the form hiding my UIWebView Contents

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

Try this code, This will work for Swift 3 . In this case, I have given 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()
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM