在方向更改期间,UIScrollView 在 SetContentSize 之后自动滚动到顶部 - 如何防止这种情况发生?
我有一个 UIScrollView,其中启用了仅垂直滚动。我正在显示按钮网格(类似于照片查看器图像网格)。必须根据屏幕方向以不同方式绘制网格,以便使用所有屏幕空间。考虑到按钮的大小,我可以在纵向每行放置 3 个按钮,在横向每行放置 4 个按钮。
我将所有按钮重新放置在: willRotateToInterfaceOrientation:duration 中,然后在 UIScrollView 上调用: setContentSize: 。一切似乎都工作得很好,除了调用 SetContentSize: 后发生的自动滚动之外。换句话说,假设我处于纵向模式,并向下滚动列表的 3/4,当我旋转到横向时,它会自动滚动所有内容到顶部。
有趣的是,在相同的场景中,如果我向上或向下轻轻滚动,然后立即旋转设备,滚动视图将正确重绘,并保留当前的滚动位置!
因此,需要明确的是,这里的罪魁祸首似乎是 SetContentSize:,但显然我必须调用它才能使滚动视图正确滚动。
关于如何保留当前滚动位置有什么想法吗?
谢谢!
I have a UIScrollView in which vertical only scrolling is enabled. I'm displaying a grid of buttons (similar to the photo viewer image grid). The grid has to be drawn differently based on screen orientation, so that all of the screen real estate is used. Given the size of my buttons, I can fit 3 per row in portrait, and 4 per row in landscape.
I reposition all of the buttons in: willRotateToInterfaceOrientation:duration and then call: setContentSize: on my UIScrollView. Everything seems to work just fine, with the exception of the auto-scrolling that occurs after the call to SetContentSize:. In other words, let's say I was in portrait, and scrolled 3/4 of the way down the list, when I rotate to landscape, it auto-scrolls all the back up to the top.
The interesting thing is, in the same scenario, if I were to do a small flick scroll up or down, and then immediately rotate the device, the scroll view redraws correctly, and retains the current scroll position!
So, to be clear, the culprit here seems to be SetContentSize:, but obviously I have to call that for the scroll view to scroll correctly.
Any ideas on how I can retain the current scroll position?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试实现
UIScrollViewDelegate
方法scrollViewShouldScrollToTop:
,它告诉调用者是否滚动到顶部。您的委托中可能必须有一些标志来指示旋转是否正在进行,因为在正常情况下,您可能实际上希望能够点击状态栏并使滚动视图滚动到顶部。另一方面,如果您不希望滚动视图自动滚动到顶部,只需实现该委托方法并让它返回 NO 即可。
You might try implementing the
UIScrollViewDelegate
methodscrollViewShouldScrollToTop:
, which tells the caller whether to scroll to the top or not. You may have to have some flag in your delegate that indicates if a rotation is underway or not, because under normal conditions you may actually want the ability to tap the status bar and have the scroll view scroll to the top.If, on the other hand, you don't ever want the scroll view to scroll to the top automaticlly, simply implement that delegate method and have it return NO.
我遇到了这个问题,只是这样做了,它对我来说效果很好
在我的例子中,我有一个滚动视图,其宽度固定到设备大小,高度可变
I had this problem, just did this and it works well for me
In my case, I have a scrollview with a fixed width to device size, and variable height