使用UIScrollView经常会执行清空视图的操作,我们普遍的做法是:
1
2
3
|
for
(UIView *subview in self.scrollView.subviews) {
[subview removeFromSuperview];
}
|
或者是:
1
|
[self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
|
但是有没有发现,这样做以后,当你重新增加内容后,再次滑动UIScrollView时,滚动条不见了!!
所以,请不要用以上方式清空视图,而应该将加入UIScroView的内容加上你的标记,删除时过滤一下即可:
1
2
3
4
5
6
7
8
9
10
11
12
|
self.v1.tag = kWCScrollViewCustomItemViewTag;
[self.scrollView addSubview:self.v1];
//...add more items
for
(UIView *subview in self.scrollView.subviews) {
if
(subview.tag == kWCScrollViewCustomItemViewTag) {
[subview removeFromSuperview];
}
}
|