55

I'm using custom UITableViewCells inside my UITableView. Each of these UITableViewCells is pretty high and contains a UITextField at the top.

When a user taps the UITextField in order to edit it, a keyboard appears and the UITableView scrolls automatically so that the cell is at the top of the screen.

The problem is that this scrolls the UITableView to the bottom of the UITableViewCell, not the top. When the UITableViewCell is high and edited the UITextField is at the top so you can't see the UITextField. I know how to scroll the UITableView programmatically, but I just don't know how to disable this automatic scrolling so that I can scroll the UITableView on my own. How can I do this?

Bono
  • 4,757
  • 6
  • 48
  • 77
animal_chin
  • 6,610
  • 9
  • 37
  • 41

9 Answers9

73

The autoscroll-behavior is located in the UITableViewController functionality.

To disable the automatic scrolling I found two ways:

  1. Use instead of the UITableViewController simply a UIViewController - set the datasource and delegate on your own.
  2. Override the viewWillAppear method and don't call [super viewWillAppear: animated]

With both solution you disable not only the Autoscroll, but also some other nice but not essential features, that are described in the overview of Apple´s class reference:

https://developer.apple.com/documentation/uikit/uitableviewcontroller

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dominic Sander
  • 2,714
  • 1
  • 18
  • 29
  • 3
    I had the same problem and solution 2 worked. I've tested this in iOS 5.0.0 and upwards and it holds so far (current release is 6.1.2). I can't vouch for it in 4 but hey, it's 2013, so we can probably say 5.0 is the lowest supported os right now. – Cocoadelica Mar 01 '13 at 09:53
  • Problem with this is that the insets are not adapted to accommodate the keyboard, at least not in iOS 7.0.6. This means that more code will be needed to do this by hand: catching keyboard notifications, ... Ugly. – meaning-matters Mar 05 '14 at 20:41
  • Can't believe I didn't know about this till today.. I was having problems with TPKeyboardAvoiding.. Thanks!! – Fidel López May 20 '15 at 21:06
  • Point no2 works on 9.1. Good answer @Dominic. Thanks – pkc456 Nov 02 '15 at 13:26
  • Option 2 is so weird. I suddenly had the automatic scrolling disable, and I didn't have super view will appear thingy XDXDXD. How could that change everything LOL – coolcool1994 Aug 05 '16 at 14:24
  • 9
    I wouldn't ever override viewWillAppear: without calling super. UITableViewController subclasses UIViewController, and Apple "requires" all UIViewController subclasses to call super. From the [UIViewController documentation](https://developer.apple.com/reference/uikit/uiviewcontroller/1621510-viewwillappear): "If you override this method, you must call super at some point in your implementation." – ryanipete Oct 13 '16 at 22:08
  • i am using `UITableViewController` if i don't use `supper.viewWillApper()` function then my footer is not coming on the top of `keyBoard` what should i do for that `Means i want to stop auto scrolling and want footer view on the top of keyboard while editing` – Pushpendra Apr 05 '17 at 09:56
8

Define properties for your UITableViewController:

@property (nonatomic) BOOL scrollDisabled;
@property (nonatomic) CGFloat lastContentOffsetY;

Before you call becomeFirstResponder:

// Save the table view's y content offset 
lastContentOffsetY = tableViewController.tableView.contentOffset.y;
// Enable scrollDisabled
scrollDisabled = YES;

Add the following code to your table view controller:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.scrollDisabled) {
        [self.tableView setContentOffset:CGPointMake(0, lastContentOffsetY)];
    }
    ...
}

After you call resignFirstResponder, set scrollDisabled = NO.

Or Arbel
  • 2,965
  • 2
  • 30
  • 40
william-yang
  • 112
  • 1
  • 2
  • This isn't a scrollview problem, is a tableViewController class. The correct answer was the override viewWilAppear – Sophy Swicz Aug 02 '17 at 12:49
5

You can do the following:

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidShow:)
                                                 name:UIKeyboardDidShowNotification object:nil];
}

- (void)unregisterForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    self.tableView.scrollEnabled = NO;
}

- (void)keyboardDidShow:(NSNotification *)notification
{
    double delayInSeconds = 0.3;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            self.tableView.scrollEnabled = YES;
    });
}

Then implement this UIScrollViewDelegate method

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (! self.tableView.scrollEnabled)
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
}

!!! But be warned, that if the user taps in a location in the UITextField that will be covered by the Keyboard, then it won't scroll.

From my point of view, the best thing to do is to make sure that all the cells from top to then one with the UITextField included, will be visible when then Keyboard will show.

arturgrigor
  • 9,361
  • 4
  • 31
  • 31
4

The best way is to subclass UITableView and then override setContentOffset(_ contentOffset: CGPoint, animated: Bool) and not to call super.setContentOffset(_ contentOffset: CGPoint, animated: Bool). In this method is where the view controller is doing the automatic scroll.

override func setContentOffset(_ contentOffset: CGPoint, animated: Bool)
{
    //Do nothing
}
Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
juanelomx
  • 251
  • 3
  • 4
3

You could disable the automatic content inset adjustment like so:

tableView.contentInsetAdjustmentBehavior = .never
Servus7
  • 358
  • 4
  • 9
2

The issue for me was not so much that it scrolled but that it took the text view being edited off the screen.

So instead of preventing the scrolling, I just rescroll the tableview to where I want when the editing is triggered, like this:

public func textViewShouldBeginEditing(textView: UITextView) -> Bool {            
  dispatch_async(dispatch_get_main_queue()) {
    tableViewController.tableView.scrollToRowAtIndexPath(self.indexPath!, atScrollPosition: UITableViewScrollPosition.Middle, animated: true)
  }
  return true
}
infinite-loop
  • 882
  • 11
  • 17
1

Unfortunately, overriding -viewWillAppear: doesn't work for me in iOS 8.

Here is my solution (as in UITableViewController implementation):

- (void)viewDidLoad {

[super viewDidLoad];

[[NSNotificationCenter defaultCenter] removeObserver:self.tableView name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self.tableView name:UIKeyboardWillHideNotification object:nil];
}

Since the auto-scrolling behaviour is invoked by UIKeyboard's show/hide notifications, so just NOT observe them.

Robert
  • 5,278
  • 43
  • 65
  • 115
Ke Yang
  • 881
  • 1
  • 12
  • 21
-2

Did you try to set "scrollsToTop" - tableview's property to NO. By default it is YES.

cocoakomali
  • 1,346
  • 1
  • 8
  • 7
-5

You can try doing the following:

self.tableView.scrollEnabled = NO;

This should disable the scrollview in the tableview.

Arash Milani
  • 6,149
  • 2
  • 41
  • 47
DHShah01
  • 553
  • 1
  • 7
  • 16
  • 1
    This does not prevent the automatic scrolling the UITableViewController does when tapping on a text field within a UITableViewCell – Kudit Nov 28 '12 at 19:46