15

I have a UITableViewController with around 20 static cells, some of these cells have UITextFields within them and some are just for selecting with a checkmark. The table is about 1.5 views worth so scrolling is required to get at the lower text fields.

When I click within a textfield at the bottom of the table the keyboard pops up as it should but this then appears over the cell/textfield.

I was under the impression (From Apple docs and elsewhere) that the UITableViewController class handles scrolling of the view automatically when a keyboard appears in any orientation and shifts the tableview up so that the cell is visible, this isn't happening though.

IOS 5.1, iPad Portrait.

4
  • 1
    possible duplicate of How to scroll the UITableviewcell scroll up when the keyboard appears?
    – rishi
    Jun 28, 2012 at 11:38
  • 1
    Not a dup IMO, this one deals with Static Cell TVC's
    – LJ Wilson
    Jun 28, 2012 at 11:46
  • 1
    The link that Rishi pointed too actually worked for my static table content. But like I said, I though this was meant to work automatically when using UITableViewController without the need to programatically manage scrolling.
    – GeoffCoope
    Jun 28, 2012 at 12:10
  • 1
    Ive found the cause of the problem. My app launches a popover, in that popover there is a button that open a full screen model view UITableViewController which contains the table. If I don't use a popover in the heirarchy then the TVC will auto scroll the table when a field is clicked, with the popover back in the hierarchy then it loses the scroll functionality of the TVC. Not sure how to fix this yet but its some progress.
    – GeoffCoope
    Jun 28, 2012 at 13:13

4 Answers 4

88

Make sure that if you are overriding viewWillAppear that you call

[super viewWillAppear:animated];

If you don't, the Scroll View will not scroll up properly.

Swift

super.viewWillAppear(animated)
11
  • 2
    This worked for me (and I should have been calling this anyway). Thanks.
    – Snips
    Aug 6, 2012 at 14:22
  • 2
    Oh! Thank you so much, I thought I would never make it work automatically again)
    – DanSkeel
    Nov 16, 2012 at 9:41
  • You know what...You are God for me !
    – Maulik
    May 13, 2013 at 7:28
  • ThnQ @EIJay It saved my time
    – ashokdy
    Oct 11, 2013 at 7:23
  • 1
    That's a good example why you should always call super, especially if this explicitly specified in the documentation ))
    – Stas
    Jun 21, 2016 at 14:52
4

I found non of these answers to be correct. After a while, I notice that if you push a controller it won't work ... but if you present it modally.. the table will automatically scroll to the used textfield.

Hope that saves time and stress to anyone.

1
  • I had a similar issue where I am using a container view.
    – RyanJM
    Jul 28, 2015 at 19:39
2

If the auto scroll of UITableViewController doesn't work with the UITextFields in cells or scroll weirdly, do these steps. Swift 5 iOS 13.2 tested 100%

First implement viewWillAppear but don't call super.viewWillAppear (this will stop auto scroll)

override func viewWillAppear(_ animated: Bool) {

}

Then let's do the scroll manually.

override func viewWillAppear(_ animated: Bool) {

  //Notification center observers
  NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)),
                                             name: UIResponder.keyboardDidShowNotification, object: nil)
  NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)),
                                            name: UIResponder.keyboardWillHideNotification, object: nil)

}

//keybord show action
@objc func keyboardWillShow(notification: Notification) {
    tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: notification.getKeyBoardHeight, right: 0)
}
//keyboard hide action
@objc func keyboardWillHide(notification: Notification) {
    tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}

extension Notification {

    var getKeyBoardHeight: CGFloat {

        let userInfo: NSDictionary = self.userInfo! as NSDictionary
        let keyboardFrame: NSValue = userInfo.value(forKey: UIResponder.keyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.cgRectValue
        return keyboardRectangle.height
    }

}

override func viewWillDisappear(_ animated: Bool) {
   super.viewWillDisappear(animated)
   NotificationCenter.default.removeObserver(self)
}
1

I ran into this issue myself. I just converted my view controller from a UIViewController to a UITableViewController in addition to adding the [super viewWillAppear:animated]; call, you will need to remove these lines:

[self.tableView setDataSource:self]; [self.tableView setDelegate:self];

As they are no longer needed and setDelegate interferes with the keyboard scrolling behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.