how to remove subviews from scrollview?

后端 未结 8 2234
时光取名叫无心
时光取名叫无心 2020-12-02 09:10

how do i remove all subviews from my scrollview...

i have a uiview and a button above it in the scrollview something like this....

here is my code to add sub

8条回答
  •  难免孤独
    2020-12-02 09:20

    An old question; but as it's the first hit on Google for this I thought I'd also make a note that there's also this method:

    [[myScrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    

    You can't do the isKindOfClass check with this, but it's still a good solution to know about.

    Edit: Another point to note is that the scrollbar of a scrollview is added as a subview to that scrollview. Thus if you iterate through all the subviews of a scrollview you will come across it. If removed it'll add itself again - but it's important to know this if you're only expecting your own UIView subclasses to be in there.

    Amendment for Swift 3:

    myScrollView.subviews.forEach { $0.removeFromSuperview() }
    

提交回复
热议问题