UIScrollView - 为新的 contentSize 设置适当的 contentOffset 会产生不需要的空白空间
我现在已经和 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过以下方法解决此问题:
所以,实际上,将所有内容都降低了 480 点
You could solve this by :
So, in effect, moving everything down 480 points
原点始终为
(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 by480 points
, add the new view at(0,0)
and set the contentOffset to(0,480)
(and the contentSize to(320,960)
.