1

I have the same problem with the one below.
https://stackoverflow.com/questions/26327764/strange-behavior-on-scroll-uitableview#=

a busy cat

A chat application needs UITableView to scroll to the bottom.
But the table is not scrolled to the last row but 5-6 rows above.

I have already tried using contentOffset and scrollToRowAtIndexPath but both cannot resolve this problem.

I guess that some code should be added to viewDidLayoutSubviews() because I am using auto layout, but I have no idea.
Do you know how to correct this issue?

scrollToRowAtIndexPath

let indexPath = NSIndexPath(forRow:(self.tableView.numberOfRowsInSection(0)-1),
     inSection: self.tableView.numberOfSections()-1 as Int)

self.tableView.scrollToRowAtIndexPath(indexPath,
     atScrollPosition: UITableViewScrollPosition.Bottom,
     animated: true)

contentOffset

let offset = CGPoint(x: 0, y: self.tableView.contentSize.height
                              - self.tableView.frame.size.height
                              + self.tableView.contentInset.bottom)
self.tableView.contentOffset = offset
Community
  • 1
  • 1
user3225917
  • 2,991
  • 2
  • 13
  • 17
  • This is not worth an 'answer', so i'll put it here; I have had this problem aplenty and always overcome it with the heightForFooter for the last section. (one of the tableView delegate methods) I don't return a view or title for footer, just set some empty pixels with it. I know it feels hacky.. – Jef Jan 11 '15 at 08:42
  • Yes, I thought of the way to overcome it too. I will try it when I can get no ways to fix it. – user3225917 Jan 11 '15 at 10:46
  • @jeff, where do you use heightOfFooter in the scrollToRowAtIndexPath func? – dancingbush Apr 09 '15 at 20:13

1 Answers1

0

You need to set the contentInset and scrollIndicatorInsets properties:

// adjust t, l, b and r
self.tableView.contentInset = UIEdgeInsets( top: t, left: l, bottom: b, right: r)
self.tableView.scrollIndicatorInsets = UIEdgeInsets( top: t, left: l, bottom: b, right: r)

Note that you'll also need to adjust these properties when the keyboard shows / hides.

lassej
  • 6,256
  • 6
  • 26
  • 34
  • It doesn't work. And I don't think it affects this for this issue. Could you explain it more? – user3225917 Jan 11 '15 at 10:43
  • My guess is that you have a text field at the bottom of the screen and that this text field doesn't scroll. If this is not the case then my answer doesn't apply. You need to notifiy the table view that it cannot use the space used by the text field by setting the bottom value of the contentInset to the height of your text field. You may also need to set the `automaticallyAdjustsScrollViewInsets` property of the view controller to `false`. – lassej Jan 11 '15 at 11:45
  • I think it's not the issue here. I found an interesting link.http://stackoverflow.com/questions/25686490/ios-8-auto-cell-height-cant-scroll-to-last-row It says it's due to Apple's bug.. – user3225917 Jan 11 '15 at 14:34