[ACCEPTED]-UIScrollView works as expected but scrollRectToVisible: does nothing-uiscrollview

Accepted answer
Score: 117

Over a month later, and I finally figured 13 it out. While the alternative above worked 12 well, it has been bugging me and I had to 11 finally figure it out. Turns out that it 10 was somewhat of a stupid mistake.

Here's 9 the old code in my viewDidLoad, where I 8 set up the scrollView:

[clefScrollView setContentSize:CGSizeMake(clefScrollView.contentSize.width, clefView.frame.size.height)];

The value of a scrollView 7 height or width can't be 0! I think this 6 got past me because I was assuming that 5 ScrollView sizes start out with a valid 4 size, and I was missing the fact that one 3 dimension was zero!

This works:

[clefScrollView setContentSize:CGSizeMake(clefView.frame.size.width, clefView.frame.size.height)];

Hope this 2 helps someone out there. I definitely spent 1 way too much time trying to debug this.

Score: 51

Yeah, I have not had success with scrollRectToVisible:animated:, but 3 setContentOffset:animated: always works for me. Out of curiosity, why 2 do you not want to use setContentOffset:animated:? It seems to be 1 the proper solution.

Score: 5

You might want to check and see that the 7 scrollView's delaysContentTouches property is set to NO.

If you 6 call the method scrollRectToVisible or setContentOffset from a touch within 5 a subview of your scrollView then your scrollView 4 could be holding things up by delaying touches.

So, make 3 sure you set the following in your scrollView.

scrollView.delaysContentTouches = NO;

You 2 will most likely be able to move the scrollView 1 with and without animation set to YES.

Score: 1

i had it like this and it didn't work

scrollView.contentSize = CGSizeMake(scrollView.contentSize.width, someKnownValue);

i was 6 just changing the height of the contentSize, so I didn't 5 think that would give me any problem but 4 it did ...

had to change it to this

scrollView.contentSize = CGSizeMake(someOtherKnownValue, someKnownValue);

turns 3 out scrollView.contentSize.width is not necessarily set to a valid value 2 from the get go, so better give it an specific 1 value

Score: 0

The suggested solution above may still give 6 an error if you haven't set the frame for 5 the "clefScrollView".

If the solution above 4 is used and still having the same problem, make 3 sure you have initialized your scollView 2 (in this case clefScrollView) frame prior 1 to setting the content view.



    clefScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,450)];

    [clefScrollView setContentSize:CGSizeMake(clefView.frame.size.width, clefView.frame.size.height)];



Score: 0

A little late to the game but I was having 7 the same problem. Though I absolutely had 6 my scrollview's content size set correctly 5 - even excessively so for the height value. Even 4 after extensive checks to validate the frames 3 and content sizes - still wasn't working. I 2 had to adjust the "bounds" frame height, then 1 everything worked great.

Score: 0

I had the same symptoms as this issue..the 6 scrollRectToVisible was not working as intended. My 5 problem was that the scrollview on the storyboard 4 had a ambiguous constraint problem. Once 3 that was fixed the call worked just fine. It 2 is something else to check when the scrollRectToVisible 1 call isn't working as intended.

Score: 0

For me the problem was that a constraint 4 in a subview was not explicit. Check that 3 every constraint in your content is set, even 2 if you are not needing it apparently for 1 the layout.

More Related questions