Overlapping UITextField in Swift 4

I am trying to handle the scrolling of a UITextField to avoid keyboard overlap. Click here to view the current OUTPUT I am getting ] StoryBoard screenshot . I tried to scroll the view when I click on the last field of the field..ie. When I click on the password field, only the email field is scrolled, i.e. second. No scroll triggers the first and last field. How can I solve this problem?

`

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


source to share


3 answers


If you really don't want to handle the logic yourself, I recommend that you give IQKeyboardManager a try!

It's a simple CocoaPod library that you import and include in your appDelegate and well ... that's it.



It's a coding-free library so you don't have to think about it anymore as it takes over and handles repositioning of all text fields throughout the application.

0


source


You need to move the whole textbox to scroll like below.



enter image description here

0


source


There are several solutions to the keyboard overlap problem, both here and on StackOverflow and YouTube. They are all variations on handling edit events. For the lazy and reserved people (like me), I would recommend using a fixed-height footer (view with or without content) at the bottom of the scroll. I usually post different types of failures and reference information there. This footer will always "nudge" the text above the keyboard.

0


source







All Articles