在iOS 7上,我有导航控制器,并且在堆栈的顶部推送了一个新的VC。
新的VC有一个UIScrollView,它填充VC的根视图并垂直滚动。如果我稍微向下滚动,然后尝试使用“卷帘返回”/“卷帘弹出”手势,垂直滚动视图首先滚动到顶部,然后interactivePopGesture被识别,我可以左右拖动堆栈中顶部的VC。
这一切为什么要发生?我希望在“向后滑动”手势被识别之前,阻止我的滚动视图自动滚动到顶部。我该怎么做呢?
更新#1:
当我创建一个新的xcode项目时,我似乎不能重现这个bug,所以这肯定是我在原始项目中的错误。我会在找到原因后更新。
更新#2:
当interactivePopGesture被识别时,在我的垂直滚动视图上调用setContentOffset方法。调试时,我发现setContentOffset是从UIScrollViewInternal _adjustContentOffsetIfNecessary调用的。
更新#3:
在下面的场景中也会出现同样的问题:我在垂直UIScrollView中使用了UITextFields。当按下某个UITextField时,会出现一个键盘。当我想以交互方式关闭键盘时(通过在键盘上拖动滚动视图),在我释放拖动后,出现了一个小故障。UIScrollView的内容偏移量暂时设置为零,然后设置回原始内容偏移量并继续动画。UIScrollViewInternal _adjustContentOffsetIfNecessary也会调用这个不需要的contentOffset设置。
我继续使用我自己的UIScrollView子类替换了这两个场景中的UIScrollView。在该子类中,我覆盖了私有方法-(void) _adjustContentOffsetIfNecessary作为空的空方法。我的两个问题都被消除了,我找不到任何负面的后果。这不是一个解决方案,我也不会使用这种方法,因为我不知道我到底做了什么。
发布于 2017-08-11 16:51:07
我在推特上找到了关于这个的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

发布于 2020-11-21 21:26:18
我在iOS 14中遇到了同样的问题。看起来没有正常的方法来阻止_adjustContentOffsetIfNecessary的调用。在problem视图中覆盖didMoveToWindow()可以帮助我
override func didMoveToWindow() {
super.didMoveToWindow()
// window == nil when new screen pushed
if window == nil {
// Set correct offset that was before
}
}发布于 2017-10-06 10:12:41
使用带有UITableView的UIViewController,而不是UITableViewController。它解决了我的问题:)
https://stackoverflow.com/questions/23823880
复制相似问题