iOS7 UIRefreshControl changes contentInset

Fogmeister picture Fogmeister · Nov 20, 2013 · Viewed 12k times · Source

I have a UINavigationController in my app. The UINavigationBar is set to opaque and all the scroll views do not overlap underneath the bar.

In one view I have a UITableView. The frame of the UITableView is (0 0; 320 504) on my iPhone 5. i.e. the height is 568 - 64 (the height of the nav bar and status bar).

The contentInset of the UITableView is (0, 0, 0, 0). When the table view first loads the contentOffset is (0, 0).

This is fine. Works brilliantly.

I added a UIRefreshControl to the table view. This works a couple of times but then after a few times of doing pull to refresh then the content at the top gets "stuck" under the nav bar.

When this happens I inspect the contentInset and it is now (-60, 0, 0, 0).

Is there any way to stop the UIRefreshControl from changing the contentInset?

Answer

jrturton picture jrturton · Nov 20, 2013

This is probably the reason why UIRefreshControl is currently only supported on UITableViewController, rather than by addition to any scrollview (which you can get away with, in many cases).

The refresh control does its magic by tinkering with the content insets of the scrollview - particularly when it ends refreshing. Unfortunately the view controller is also tinkering with the content insets of the scroll view to fit it under the translucent nav and status bars. Fun ensues. Is this also an issue on iOS 6 (or, "good old iOS6" as I called it when dealing with the same issue).

The quickest solution is probably to add your table view as a child UITableViewController instead of a simple subview. I think that UITableViewController manages the insets for you at the end of the refresh. If that doesn't work, I've got workarounds for this but it will have to wait until I get back in the office.