开发者

Clear content of UIScrollView

开发者 https://www.devze.com 2023-01-03 15:58 出处:网络
I have an iPhone application that uses a timer and at every time interval it creates some UILabel controls and adds them into a UIScrollView.

I have an iPhone application that uses a timer and at every time interval it creates some UILabel controls and adds them into a UIScrollView.

What I want to do is to clear the UIScrollView every time interval before it puts the UILabel controls into the UIScrollVie开发者_StackOverflow社区w.

How can I clear the contents of the UIScrollView?


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


You will also remove the scrollbar with the above solution. To solve this, you may ask whether the subview about to be removed is a UIImageView instance. Obviously, you will need to do more checks if you happen to have your own UIImageViews in the scroll view.

for (UIView *subview in scrollView.subviews) {
  if(![subview isKindOfClass:[UIImageView class]])
    [subview removeFromSuperview];
}


I typically go with a one liner like this: in case you're interested..

while(scrollView.subviews.count > 0) [[scrollView.subviews objectAtIndex:0] removeFromSuperview];


Its Very simple just write this code inside your loop where dynamic structure is creating and changing it's content frequently.

[subview removeFromSuperview];


In one line

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


SWIFT:

 for  subview in self.scrollView.subviews
        {
             subview.removeFromSuperview()
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消