IOS开发-UIScrollView陷阱之----删除所有子view, 滚动条(indicator) 消失

时间:2022-06-01 20:20:27

使用UIScrollView经常会执行清空视图的操作,我们普遍的做法是:

 for (UIView *subview in self.scrollView.subviews) {
[subview removeFromSuperview];
}

或者是:

    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

但是有没有发现,这样做以后,当你重新增加内容后,再次滑动UIScrollView时,滚动条不见了!!

所以,请不要用以上方式清空视图,而应该将加入UIScroView的内容加上你的标记,删除时过滤一下即可:

    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];
} }