UIScrollView - 为新的 contentSize 设置适当的 contentOffset 会产生不需要的空白空间

发布于 2024-12-08 05:15:34 字数 1076 浏览 1 评论 0原文

我现在已经和 UIScrollView 玩了一段时间了。我正在尝试的是根据各种因素将子视图插入到滚动视图中。由于我的滚动视图仅在垂直方向滚动,因此我将在当前可见视图之前或之后插入子视图。

所以,假设我当前可见视图的帧是(0,0,320,480),scrollView的contentSize是(320,480),当前contentOffset是(0,0)

当我想在currentView上方插入子视图并保持currentView处于焦点时,我在位置(0,-480,320,480)插入新的子视图并将scrollView的contentSize更改为(320, 960),同时保持 contentOffset 与 (0,0) 相同。

令人惊讶的是,UIScrollView 在 currentView 之后添加了“额外”空间,而不是将其插入到 currentView 上方。虽然上面新插入的视图永远无法成为焦点,因为 UIScrollView 可能从错误的 contentOffset 中假设了 contentSize!

我用谷歌搜索发现还有一些其他面临类似的问题,但尚未产生结果: 设置 UIScrollView 内容大小的起点

请告诉我如果我做错了什么?或者这是某种限制等?

谢谢!

编辑:一些 线程 建议更改 contentSize会影响 contentOffset 属性,因此为了确保这不会导致问题,我仅在更改 contentSize 后才更新 contentOffset 属性。即使这样我也面临着同样的问题。

I have been juggling for a while with UIScrollView now. What I am trying is to insert subviews into the scrollView based on various factors. Since I have scrollview to scroll only in vertical direction, I would insert the subview before or after the current visible view.

So, lets say my current visible view's frames are (0,0,320,480), the contentSize of scrollView is (320,480) and current contentOffset is (0,0)

When I want to insert subview above the currentView and yet keep the currentView in focus, I insert the new subview at the position (0,-480,320,480) and change the contentSize of scrollView to (320, 960) while keeping the contentOffset same as (0,0).

The surprising thing which happens is, UIScrollView adds "extra" space after the currentView instead of inserting it above the currentView. While the newly inserted view above can never be brought into focus coz UIScrollView is assuming the contentSize from a wrong contentOffset perhaps!

I googled and found that there are some others facing similar problems, but did not yield results yet:
Set starting point of content size for UIScrollView

Please let me know if I am doing anything wrong? Or is this some kind of limitation etc?

Thanks!

Edit: Some threads suggests that changing the contentSize will affect contentOffset property, so to make sure that this is not causing problem, I am updating the contentOffset property only after I change the contentSize. Even then I am facing same problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哭了丶谁疼 2024-12-15 05:15:34

您可以通过以下方法解决此问题:

  • 添加带有框架 (0,0,320,480) 的新子视图
  • ,将现有子视图的框架设置为 (0,480,320,480),
  • 将 contentSize 设置为 (320,960),
  • 将内容偏移量设置为 (0,480) - 当然不是动画。

所以,实际上,将所有内容都降低了 480 点

You could solve this by :

  • adding your new subview with frame (0,0,320,480)
  • setting the frame of your existing subview to (0,480,320,480)
  • setting the contentSize to (320,960)
  • setting the content offset to (0,480) - not animated, of course.

So, in effect, moving everything down 480 points

蓝眼泪 2024-12-15 05:15:34

原点始终为(0,0)。如果您想在当前偏移量之上插入一些内容,您需要将当前视图向下移动 480 点,在 (0,0) 处添加新视图并设置contentOffset 为 (0,480)(contentSize 为 (320,960))。

The origin is always (0,0). If you want to insert something above the current offset you'll want to move the current views down by 480 points, add the new view at (0,0) and set the contentOffset to (0,480) (and the contentSize to (320,960).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文