0

I'm new in Swift 3, and I just notice that we need to scroll a view since the keyboard can go over the some widgets as we type in text fields, like in this video. Fixing UITextField-Keyboard Problems (Swift in Xcode).

However, I am having an unfortunate behaviour even before programmatically scrolling the view. My view keeps being reset to its initial position. I made a video showing this behaviour.

I can't find mentions for this problem. Would someone have a clue?

[EDITED]

Here is the widgets are structured

enter image description here

Here is my View Controller code:

import UIKit
import AVFoundation

class SignUpController: UIViewController, UITextFieldDelegate
{

    @IBOutlet var scrollView: UIScrollView!
    @IBOutlet var stackView: UIStackView!

    @IBOutlet var usernameTextField: UITextField!
    @IBOutlet var passwordTextField: UITextField!
    @IBOutlet var confirmationTextField: UITextField!
    @IBOutlet var emailTextField: UITextField!
    @IBOutlet var phoneTextField: UITextField!
    @IBOutlet var firstNameTextField: UITextField!
    @IBOutlet var lastNameTextField: UITextField!

    @IBOutlet var signUpButton: UIButton!

    var keyboardHeigh:CGFloat!

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.scrollView.contentSize = self.stackView.frame.size
        self.scrollView.contentSize.height += self.stackView.frame.minY + 20
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let textFields :[UITextField] =
            [
                usernameTextField,
                passwordTextField,
                confirmationTextField,
                emailTextField,
                phoneTextField,
                firstNameTextField,
                lastNameTextField,
            ]

        for i in 0..<textFields.count
        {
            textFields[i].delegate = self
            textFields[i].tag = i
        }

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(keyboardWillShow),
                                               name: .UIKeyboardWillShow, object: nil)
    }

    func keyboardWillShow(notification: NSNotification)
    {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue
        {
            keyboardHeigh = keyboardSize.height
        }
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool
    {
        // Try to find next responder
        if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField
        {
            nextField.becomeFirstResponder()

        }
        else
        {
            // Not found, so remove keyboard.
            textField.resignFirstResponder()

        }

        return false
    }
}
Eduardo Reis
  • 1,691
  • 1
  • 22
  • 45
  • 1
    Post your code... Maybe you're scrolling programmatically in the wrong place. – Pintouch Apr 28 '17 at 12:57
  • @Pintouch, Code posted. – Eduardo Reis Apr 28 '17 at 13:25
  • 2
    I'm betting your problem starts with auto-layout and constraints. The Red-Circle with White-Arrow icon indicates your constraints are *not* set up correctly. Add in the fact that you are trying to set the `scrollView.contentSize` via code, and you are further messing things up. When you tap into a new field, the auto-layout engine "resets" the scroll view to its proper position, based on the (faulty) constraints. – DonMag Apr 28 '17 at 13:45
  • 1
    You can check [this post](http://stackoverflow.com/questions/20909673/how-to-make-the-view-controller-scroll-to-text-field-when-keyboard-appears) ( it's objective-c but the iOS workflow is explained). In my opinion if you don't want to manage the scrollView, maybe it's a better idea to use a UITableViewController adding your TextField in the cells. (You just have to custom the cells hiding the borders) And you will not have to worry about the keyboard. – Pintouch Apr 28 '17 at 14:03
  • I just noticed that `viewDidLayoutSubviews` is called twice whenever I select a `UITextField`. I was adjusting my `UIScrollView` on `viewDidLayoutSubviews` because of the tutorial I was following in order to get my `UIScrollView` to scroll instead of only bounce. I just tried to move the snippet bellow to `viewDidLoad` and it worked fine! `self.scrollView.contentSize = self.stackView.frame.size; self.scrollView.contentSize.height += self.stackView.frame.minY + 20;` But I am still willing to fix my constraints, as @DonMag explained it has problems. – Eduardo Reis Apr 28 '17 at 14:04
  • This is the post [tutorial][http://stackoverflow.com/a/24686042/2313889] I mentioned above that got my `UIScrollView` to actually scroll instead of only bouncing. – Eduardo Reis Apr 28 '17 at 14:10

1 Answers1

1

1.You can take UItableViewController with static cell,

2.Put textField inside cell.

3.Make selection for cell none.

4.Make separator none.

It will work fine.

Shabbir Ahmad
  • 615
  • 7
  • 17