1

I am using a normal UITableViewController with static cells(1 cell). Having several UITextfields in it,inside StackView Since it is a UITableView, the scrolling is handled automatically but the problem is that it scrolls to random positions when a UITextfield is clicked.

This is the First Screen This is the Second Screen This is the Third Screen This is the Fourth Screen

The first screenshot is the page without a keyboard. The second screenshot is when First Name textfield is clicked (normal behavior) The third screenshot is when Last Name textfield is clicked (??!! behavior) The fourth screenshot is when Email textfield is clicked (again ???!! behavior)

I am not using any external keyboard handling libraries

I am not using any code to add insets or handle scrolling or handle the keyboard.

The Textfields are placed in correct order(TxtFname,TxtLname,TxtFatherName....)

The UITextfields are placed inside a stackview. Can someone tell me what seems to be the problem?

Zyfe3r
  • 653
  • 6
  • 24
  • Which library you are using for keybaord? – AyAz Jan 16 '20 at 06:22
  • @AyazAkbar No libraries are being used. – Zyfe3r Jan 16 '20 at 06:30
  • Then use IQkeyboardManager. This will handle automatically – AyAz Jan 16 '20 at 06:43
  • @AyazAkbar Tried using it, no change. Its still going randomly ! – Zyfe3r Jan 16 '20 at 07:06
  • func textFieldShouldReturn(_ textField: UITextField) -> Bool { let nextTag = textField.tag + 1 if let nextResponder = textField.superview?.viewWithTag(nextTag) { nextResponder.becomeFirstResponder() } else { textField.resignFirstResponder() } return true } – AyAz Jan 16 '20 at 07:10
  • upvote as its a genuine question to be asked – 9to5ios May 26 '20 at 09:07
  • @Zyfe3r have you solved this issue? I'm facing the same. I'm using tableview in a viewController, and no 3rd party library. I have cells that have textfield in them. I've disabled all keyboard event observer. Still having this random scrolling action. Suspect it could be estimatedRowHeight? – Bonan Nov 18 '20 at 05:34

5 Answers5

0

You can use the third party to handle the keyboard with scrolling of a UIView or a UITableview.
pod 'IQKeyboardManager' This the third party which can help you manage the scrolling. If you don’t want to scroll the header make it in different view apart from table view. And make sure you have use the auto layouts properly.

func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
    if tblProfile.isDecelerating {
        view.endEditing(true)
    }
}

Use this code when you are scrolling the tableview manually without using the keyboard to go on next text field this method will be called and will hide the keyboard. Once you tap on textfield the keyboard appears again.

Apps Maven
  • 1,314
  • 1
  • 4
  • 17
0

You can go over my previous solution. It exactly handle the issue described.

moving-keyboard-when-editing-multiple-textfields-with-constraints-swift

Vasucd
  • 357
  • 2
  • 10
  • Isnt UITableView supposed to handle the scrolling properly? Or is it bugged or am i doing something really wrong? Also the VIEW DOES MOVE UP to CORRECT position if the keyboard isnt on the screen. If i press Fname , it scrolls to correct position. If i dismiss keyboard and click on Lname , it scrolls to correct position. BUT, if i click Lname while keyboard is already on screen, it scrolls to bottom of the screen! – Zyfe3r Jan 16 '20 at 06:33
  • So what ideally needs to happens is when you focus on any text field . your view (tblview) bottom constant should be at height of keyboard and when tap done button bottom constraint should be at its original position . Can you show some code for keyboarding opening and dismissals. – Vasucd Jan 16 '20 at 06:44
  • Also when you working on this with out third party tool . you need to understand the cases like when this UIView needs to shift or not . let's say you one field on top and you are shifting your UIView which makes disappear your field that's does not make any sense . – Vasucd Jan 16 '20 at 06:48
  • There is literally NO code for keyboard opening and dismissal , other than a little hack to dismiss keyboard. I just dragged a ``DidEndOnExit`` outlet for each UITextfield and left it empty. So anytime user clicks on return, the keyboard disappears – Zyfe3r Jan 16 '20 at 06:48
  • try your case for bottom textfield and on small screen size device like iPhone 4s / 5 . The you will understand shifting UIView issue. – Vasucd Jan 16 '20 at 06:52
  • Shifting issue only happens in ViewController with UITableview in it, doesn't happen incase of TableViewControllers. Since the entire scrolling is automatically handled. Right?? – Zyfe3r Jan 16 '20 at 06:59
  • I've never tried tableview controller but always used uitableview inside ViewController and faced the issue multiple times .I've suggest you the ideal way. – Vasucd Jan 16 '20 at 07:11
  • 1
    Thanks for trying to help. But this isnt what i was looking for. I do know about keyboardwillhide and keyboardwillshow notification stuff. I have used it before :),incase of tableview inside viewcontroller. I used TableViewController because i just want to have a since non repeating cell aka, static cell, which can scroll like scrollView without having to putup with issues faced from ScrollViews. – Zyfe3r Jan 16 '20 at 07:14
0

I have made a band-aid fix for this problem , combing different answers posted here and little bit extra.

 override func viewWillAppear(_ animated: Bool) {
           //  super.viewWillAppear(animated)
         }

Commenting the viewWillAppear(animated) disables the automatic scrolling of UITableViewController.

Now 2 notification observers are added in ViewDidLoad()

      NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)


        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)

and their function as

@objc func keyboardWillShow(notification: NSNotification) {
        if !isKeyboardShowing {
            if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
                let keyboardHeight = keyboardSize.height

                let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber
                let curve = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber

                adjustTableViewInsets(keyboardHeight: keyboardHeight, duration: duration, curve: curve)
            }
        }
    }

    @objc func keyboardWillHide(notification: NSNotification) {
        let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber
        let curve = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber
        adjustTableViewInsets(keyboardHeight: 0, duration: duration, curve: curve)
    }

This will make sure that there is extra space for the user to scroll to the bottom of the page even if keyboard is visible.

Now i used IQKeyboardManager for the extra functionality of UP n DOWN arrows to move to next UITextField and DONE button to dismiss keyboard.

Zyfe3r
  • 653
  • 6
  • 24
0

Hi face similar issue using

UITableview with custom cell (having xib and a number of textfield) Having dropdown() and IQKeyboardManager (third party used )

Easily fix it

Inside viewDidload you just put

yourTextField.inputView.inputView = UIView()
yourTextField.inputView.inputAccessoryView = UIView()
9to5ios
  • 5,319
  • 2
  • 37
  • 65
-1

try it. I hope that will help you: https://medium.com/@hassanahmedkhan/scrolling-the-hell-out-of-stackview-33d239f9f38e or you could use uitable view like that: https://medium.com/@how_noobs_think/uitableviewcontroller-will-automatically-adjust-content-position-relative-to-keyboard-ios-swift-8810a375ffb2

or simple: Move view with keyboard using Swift

quynhbkhn
  • 59
  • 4
  • I know and tried a few of these. But the thing is, the table is scrolling and i can just scroll to bottom of the Table. The keyboard isnt making anything impossible to reach. Its just that the view scrolls in random direction. Sometimes UP, sometimes DOWN. – Zyfe3r Jan 16 '20 at 06:14