在iOS 7上。
我有导航 Controller ,并将新的VC插入堆栈顶部。
那个新的VC有一个UIScrollView,它填充了VC的根 View 并垂直滚动。如果我向下滚动一点,然后尝试使用“向后滑动”/“向弹出滑动”手势,则垂直滚动 View 首先滚动到顶部,然后识别出InteractivePopGesture,然后可以将顶部的VC拖动到左右堆叠。
为什么会这样?
我希望防止在我的“滚动后退”手势被识别之前,滚动 View 自动滚动到顶部。
我该怎么做呢?
更新#1:
当我创建一个新的xcode项目时,我似乎无法重现此错误,因此,在我的原始项目中,这肯定是我自己的错误。找到原因后会更新。
更新#2:
当InteractivePopGesture被识别时,在我的垂直滚动 View 上调用setContentOffset
方法。在调试时,我看到setContentOffset
是从UIScrollViewInternal _adjustContentOffsetIfNecessary
调用的。
更新#3:
在以下情况下,也会发生相同的问题:
我在垂直的UIScrollView中有UITextFields。当按下某个UITextField时,将出现一个键盘。当我想以交互方式关闭键盘(通过在键盘上的滚动 View 上拖动)时,松开拖动后会出现故障。 UIScrollView的内容偏移已暂时设置为零,然后又设置回原始内容偏移并继续进行动画处理。 contentOffset
也会调用UIScrollViewInternal _adjustContentOffsetIfNecessary
的此不必要的设置。
我继续将两种情况下的UIScrollView替换为我自己的UIScrollView子类。在该子类中,我将私有(private)方法-(void) _adjustContentOffsetIfNecessary
重写为空的void方法。我的两个问题都消除了,我找不到任何负面后果。这不是解决方案,因为我完全不知道自己做了什么,所以我不会使用这种方法。
最佳答案
我在Twitter上找到了关于此的interesting discussion。虽然这不是我的情况,但它可能对某人有帮助:
Is there some trick to bring UINavigationController to not mess with a UIScrollView (_adjustContentOffsetIfNecessary)?
@steipete Searching for the same thing and came across your tweet. I set contentOffset but it resets back to -64 (ios7). Did you find a fix?
@errthling Add a dummy view as first subview. It won’t look further.
@steipete Wow – that worked. I've been struggling with it for days. Thanks for that and everything else you put out there! :)
@errthling happy to help!
@steipete did you find an answer to this, 2 years ago? =D my scrollview's contentOffset is reset when I push a new viewcontroller..
@manuelmaly Yeah. Changed the view hierarchy so the scroll view is not the main view and not the first subview, and UIKit stops messing.
@steipete oO i hacked it in the meantime by blocking setContentOffset before pushViewController is called. your solution seems cleaner tho
关于ios7 - _adjustContentOffsetIfNecessary-UIScrollView自动滚动到顶部,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23823880/