UIScrollView works as expected but scrollRectToVisible: does nothing

前端 未结 8 2297
一向
一向 2020-12-14 05:46

I have used UIScrollView before, and am using it now, and never had a problem. I\'m now adding it to an old app, and while it works as expected (I can look at

相关标签:
8条回答
  • 2020-12-14 05:53

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

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

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

    scrollView.delaysContentTouches = NO;
    

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

    0 讨论(0)
  • 2020-12-14 05:53

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

    0 讨论(0)
  • 2020-12-14 06:00

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

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

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

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

    This works:

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

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

    0 讨论(0)
  • 2020-12-14 06:00

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

    0 讨论(0)
  • 2020-12-14 06:03

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

    0 讨论(0)
  • 2020-12-14 06:14

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

    If the solution above is used and still having the same problem, make sure you have initialized your scollView (in this case clefScrollView) frame prior 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)];
    
    
    
    
    0 讨论(0)
提交回复
热议问题