Moshe Moshe - 1 year ago 120
Swift Question

Remove all the subviews from a UIScrollView?

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

koo koo
Answer Source

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{
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download