Remove all the subviews from a UIScrollView?
Asked Answered
J

5

48

How do I remove all of the subviews from a UIScrollview?

Jock answered 4/11, 2010 at 23:33 Comment(1)
Swift version is herePentothal
B
127

Let scrollView be an instance of UIScrollView.

In Objective-C, it's pretty easy. Just call makeObjectsPerformSelector:, like so:

Objective-C:

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

In Swift, you don't get that runtime access, so you have to actually handle the iteration yourself.

Swift:

A concise version, from here:

scrollview.subviews.map { $0.removeFromSuperview() }

A more descriptive way to do this (from here) assumes scrollview.subviews:

let subviews = self.scrollView.subviews
for subview in subviews{
    subview.removeFromSuperview()
}
Blanket answered 4/11, 2010 at 23:47 Comment(3)
This will remove the scroll indicators if they are set to display, but the OP isn't concerned with the them as noted in the comments below.Posthaste
I think you should make a copy of the subviews array before calling this method, since you aren't supposed to call the makeObjectsPerformSelector: with a method which mutates the original array. This will.Expeditious
you should use forEach instead of map, since map returns an array.Fair
S
35

I think you just have to be careful and not to delete the scroll indicator bars.

The code given by Adam Ko is short and elegant but it may delete the horizontal and vertical scroll indicators.

I usually do

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

Suposing you don't have UIImageView's added to the scroll manually.

Sebaceous answered 5/11, 2010 at 8:11 Comment(2)
For what I've tested, my code did not remove the scroll indicators, I believe that's because UIScrollView loads indicators lazily whenever it needs to be displayed, but that's a good point to be noted though.Blanket
Actually I think this is not a good solution because what if you have UIImageView subviews that you want to remove?Character
M
2

In addition to Ricardo de Cillo's, in my case I had a table view that had imageviews in the subviews that I wanted to remove.

for (UIView *v in self.tableview.subviews) {
  if ([v isKindOfClass:[UIImageView class]]) {
    [v removeFromSuperview];
  }
}

The removal of the ! in the if command, and change scrollview to self.tableview removed all images, but left the table view untouched.

Measles answered 1/6, 2014 at 9:19 Comment(0)
P
1

If you want to remove uiimageviews in scrollview.subviews, and you also want to keep the vertical and horizontal indicators. You can set a special "tag" to identify views and exclude vertical and horizontal indicators whose tags are 0 by default.

Pyo answered 24/7, 2013 at 1:57 Comment(0)
M
0

Complementing the Swift concise version from a previous answer (Swift 3.0 ready):

_ = scrollView.subviews.filter { $0 is UIImageView }.map { $0.removeFromSuperview() }
Maller answered 13/9, 2016 at 5:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.