刷新/重新加载 UIScrollView 的最佳方法
我有一个方法可以将一些子视图(图像/按钮)添加到我的 ScrollView 中。
问题是,用户可以从 ScrollView 外部点击一个按钮来更改 ScrollView 的内容。
有没有比以下更好的“重置”ScrollView 的好方法:
for (UIView * view in myScrollView.subViews) {
[view removeFromSuperview];
}
请注意,此解决方案删除了指示 ScrollView 上位置的栏
I have a method that adds some subviews (images/buttons) to my ScrollView.
The thing is that from outside of the ScrollView the user can tap a button that will change the content of the ScrollView.
Is there any decent way to "reset" the ScrollView better than:
for (UIView * view in myScrollView.subViews) {
[view removeFromSuperview];
}
Note that this solution removes the bar to indicate the position on the ScrollView
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
调用 setNeedsDisplay 只是重绘滚动视图,而不是实际删除其中的内容。
正如您所发现的,在所有滚动视图子视图上调用
removeFromSuperview
会导致您的滚动条有时也会被删除(不太好)。有几种方法可以解决这个问题:第一种方法是维护一个不同的数组,其中包含您自己添加到滚动视图中的所有视图。然后您可以只迭代这个数组。在我看来,这可能是最好的方法。
另一种稍微有点老套的方法是在迭代视图时测试视图:
因为滚动条本身是 UIImageView 子类,所以不会删除它们。但话又说回来,如果您自己使用图像视图,它们也不会被删除,并且不能保证在未来的 iOS 版本中它们将保留图像视图。
最好采用我的第一种方法,并保留一个包含您添加的所有视图的数组。
Calling
setNeedsDisplay
is just going to redraw your scroll view, not actually remove the content inside of it.As you've discovered, calling
removeFromSuperview
on all scroll view subviews will result in your scroll bars sometimes being removed as well (not great).There are a couple of approaches around this: the first is to maintain a distinct array containing all the views you yourself have added to the scroll view. You can then just iterate through this array instead. In my view this is probably the best approach.
The other, slightly hacky, way is to test the views as you iterate through them:
Because the scroll bars are themselves a
UIImageView
subclass this will not remove them. But then again, if you're using image views yourself they won't get removed either, and there's no guarantee in future iOS versions they will remain image views.So much better to go with my first approach and just keep an array around with all the views you've added.
这是一个旧线程,但一个稍微简单的方法可能只是向您添加到 UIScrollView 的视图添加一个标签,比方说 15。这样
您就可以确保只有您添加的视图被删除。
只是一个想法。
This is kind of an old thread, but a slightly easier method might just be to add a tag to the view's your adding to the UIScrollView, let's say 15. Then
this way you can be sure that only the view's you have added get removed.
Just a thought.
我通常会保留一个单独的可变视图数组,并将其放入滚动视图中,并对其进行迭代,而不是子视图。这样我只取出我放入的内容。但基本方法是相同的。
I generally keep a separate mutable array of views that I've put into the scrollview, and iterate over that instead of
subviews
. That way I only take out what I put in. But the basic approach is the same.这是我删除除滚动条之外的所有子视图的方法,我删除了所有frame.size.width大于10的子视图,滚动条宽度小于10,所以它不会被删除。这假设您添加到 UIScollView 的所有视图的宽度都大于 10。
Here is what I did to remove all subview except scrollbar, I removed all subviews that have frame.size.width greater than 10, scrollbar width is smaller than 10, so it will not be removed. This assumes that all the views that you add to UIScollView have a width greater than 10.
删除任何不需要的视图(我假设您无法解决)后,调用
[myScrollView setNeedsDisplay];
在下一个绘图周期刷新 UIScrollView。Upon removing any unneeded views (which I'm assuming you're not going to be able to work around) call
[myScrollView setNeedsDisplay];
to refresh the UIScrollView on the next drawing cycle.您必须在启动滚动方法之前设置此行,它只是从滚动视图中删除所有数据,然后根据您的方法滚动视图将重置。
You have to set this lines before start scroll method, it's simply remove all data from scrollview and as per your method scrollview will reset.