Paging Enabled peculiarity in UIScrollView

Whilst developing an iPhone app, I have come some very odd behaviour in UIScrollView. Ok, I’ll admit what I was doing was somewhat unusual, but I think the issue could occur in less unusual circumstances.

The effect appeared to be that the UIScrollView was remembering the last user scroll page position, despite my changing the frame, contentSize and contentOffset of the view manually. If the user touched the scroll view after my programmatic changes, the view would immediately scroll back to the page that had last been user scrolled to.

Consider the following situation: we start with the scrollview (and content) looking like

[1 2 3 4] 5 6 7 8

where the brackets denote the visible page area. I manually scroll to show the second page:

1 2 3 4 [5 6 7 8] 

which works fine. I then change to “selected” mode programmatically, and the display now shows:

1 2 3 4 5 6 [7] 8

again, which works fine.
However, if I now tap on the UIScrollView it immediately scrolls to

1 2 3 4 [5] 6 7 8

for no apparent reason.

This was going to be a complete show stopper for me, so I spent quite a long time delving into the mysteries of UIScrollView, and to cut a long story short, I think I’ve found a bug. I’ve opened a bug report with and submitted a test case, but not heard anything yet, so I’ll update this post as and when they confirm or refute it.

However, the good news is that while investigating what was causing the problem, having guessed at the cause, which I think is a rounding error between two different member variables, responsible for handling the paging enabled mode, a workaround idea presented itself.

What I discovered was, that if the content view width was integer divisible by the number of pages (which in this case was 2 or 8 pages wide), the problem didn’t happen, whereas if the content size divided by the number of pages resulted in a fraction, the issue occurred. Thankfully, this was a fairly easy workaround to implement, and perhaps explains why it hasn’t shown up often enough for apple to have found it previously.

5 thoughts on “Paging Enabled peculiarity in UIScrollView”

  1. Do you have any updates on this issue? I’m running into it with the PhotoScroller sample app. The initial view after loading will be offset and if I touch it it’ll jump back to the “previous view” if there was one.

    Has Apple responded to your bug report?

    1. No update from apple since I uploaded my test case in September.

      Try and make sure your page size is an integer multiple of your content size; for me, the problem went away when it was.

  2. I got exactly the same problem when I modified Apple’s PhotoScroller for a pdf reader.

    I don’t quite understand what you mean “the page size is an integer multiple of your content size”. Is the content size the screen size for my page displaying? Is the page size actually the total size of all the pages? If that is the case, does that mean my number of page is even will not cause the problem if all the page has the same size?

    Please advise, thanks

    1. The content size is the total size of what you are viewing. For example my content was 294 wide, and my scroll view size was 73.5. When looking in the debugger, some of the scrollview properties were rounded up, and others weren’t, hence the view thought it wasn’t at a page position when rounding had occurred.

      When I changed the content size to 292 and the view size to 73, the problem went away as there was no rounding to be done.

Leave a Reply

Your email address will not be published. Required fields are marked *