¿Eliminar todas las subviews de un UIScrollView?
¿Cómo elimino todas las subviews de un UIScrollView?
5 answers
Sea scrollView
una instancia de UIScrollView
.
En Objective-C, es bastante fácil. Simplemente llama makeObjectsPerformSelector:
, así:
Objetivo-C:
[scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
En Swift, no obtienes ese acceso en tiempo de ejecución, por lo que tienes que manejar la iteración tú mismo.
Swift:
Una versión concisa, de aquí:
scrollview.subviews.map { $0.removeFromSuperview() }
Una forma más descriptiva de hacer esto (de aquí) asume scrollview.subviews
:
let subviews = self.scrollView.subviews
for subview in subviews{
subview.removeFromSuperview()
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:10:43
Creo que solo hay que tener cuidado y no eliminar las barras indicadoras de desplazamiento.
El código dado por Adam Ko es corto y elegante, pero puede eliminar los indicadores de desplazamiento horizontal y vertical.
Normalmente lo hago
for (UIView *v in scrollView.subviews) {
if (![v isKindOfClass:[UIImageView class]]) {
[v removeFromSuperview];
}
}
Suponiendo que no tiene UIImageView añadido al desplazamiento manualmente.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-05 08:11:47
Además de la de Ricardo de Cillo, en mi caso tenía una vista de tabla que tenía imageviews en las subviews que quería eliminar.
for (UIView *v in self.tableview.subviews) {
if ([v isKindOfClass:[UIImageView class]]) {
[v removeFromSuperview];
}
}
La eliminación de la ! en el comando if, y cambie scrollview
a self.tableview
eliminó todas las imágenes, pero dejó la vista de tabla intacta.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-01 09:19:47
Si desea eliminar uiimageviews en scrollview.subviews, y también desea mantener los indicadores verticales y horizontales. Puede establecer una "etiqueta" especial para identificar vistas y excluir indicadores verticales y horizontales cuyas etiquetas son 0 por defecto.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-24 01:57:50
Complementando la versión Swift concise de una respuesta anterior (Swift 3.0 ready):
_ = scrollView.subviews.filter { $0 is UIImageView }.map { $0.removeFromSuperview() }
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-13 05:58:56