Moshe Moshe - 8 months ago 73
Swift Question

Remove all the subviews from a UIScrollView?

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

koo koo

Let scrollView be an instance of UIScrollView.

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


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

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


A concise version, from here: { $0.removeFromSuperview() }

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

let subviews = self.scrollView.subviews
for subview in subviews{