UIScrollView unwanted scrolling after addSubview o

2019-05-11 17:11发布

问题:

I have a UIScrollView filled with subviews, all is well when creating it and initially filling it.

But when I add a new subview that is positionned outside of the visible screen portion, or when I just resize an existing subview that is also outside of the visible screen portion, there is a subsequent 0.3s-long scroll animation (I can see it happening from my delegate) that seems to match the newly added/resized element.

Attempts:

  • pagingEnabled is always NO.
  • Setting scrollEnabled to NO during subview manipulations doesn't help.
  • Doing a setContentOffset:animated:NO after subview manipulations doesn't prevent the animation.
  • One single giant subview with all my subviews in it doesn't help.

My current workaround is to initially set the frame to fit inside the visible screen portion, or doing resizing work inside another superview, but it feels dirty, and won't handle all situations...

Is there a way to prevent this automatic scrolling animation when programmatically manipulating subviews?

Xcode 4.3, iOS SDK for 5.1.

回答1:

I too discovered this problem and found this solution http://www.iphonedevsdk.com/forum/iphone-sdk-development/94288-disabling-uiscrollview-autoscroll.html

It involves subclassing the UIScrollView and entering no code in the following method.

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated  {

}

Like the guy says on the link I've found it works and no problems so far. Hope it works for you.



回答2:

I had this problem because I set the content size of the scroll view prior to adding the subview.

As soon as I change the code so that the content size of the scroll view was set after adding the subview the problem went away.