UIScrollView unwanted scrolling after addSubview or changing frame

♀尐吖头ヾ 提交于 2019-12-22 10:11:17

问题


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.



来源:https://stackoverflow.com/questions/11176314/uiscrollview-unwanted-scrolling-after-addsubview-or-changing-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!