keyboard overlapping UITextField in Swift 4

Viewed 5469

i am trying to handle the scrolling of UITextField to avoid the keyboard to overlap it. Click here to view current OUTPUT i am getting ]StoryBoard Screenshot. I tried to scroll the view when i click on the last textfield ..ie password field. When i click on the password field, only the email field ie the 2nd one is scrolling up. No scroll occur ion the first and last field. How can i solve this issue?

`

//
//  ViewController.swift


import UIKit

class ViewController: UIViewController,UITextFieldDelegate {

    @IBOutlet weak var scrollView: UIScrollView!

    @IBOutlet weak var name: UITextField!

    @IBOutlet weak var email: UITextField!


    @IBOutlet weak var password: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        // Begin
        switch textField.tag {
        case 0...1:
            print("DO NOTHING")
        default:
            print("Do Scroll")

            scrollView.setContentOffset(CGPoint(x:0,y:100) , animated: true)
        }

    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        // End
        scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)

    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
         // Return

        if textField.tag == 0
        {
            email.becomeFirstResponder()
        }
        else if textField.tag == 1
        {
            password.becomeFirstResponder()

        }
        else if textField.tag == 2
        {
            textField.resignFirstResponder()

        }
        return true
    }


}

`

3 Answers
Related