Answer the question
In order to leave comments, you need to log in
How to loop through child values?
Good time of the day, you need to sort through and delete all child elements, now I have such a horror:
for view1 in self._searchBackground.subviews {
for view2 in view1.subviews {
for view3 in view2.subviews {
for view4 in view3.subviews {
for view5 in view4.subviews {
for view6 in view5.subviews {
view6.removeFromSuperview()
}
view5.removeFromSuperview()
}
view4.removeFromSuperview()
}
view3.removeFromSuperview()
}
view2.removeFromSuperview()
}
view1.removeFromSuperview()
}
self._searchBackground.removeFromSuperview()
Answer the question
In order to leave comments, you need to log in
Use recursion. I am not familiar with swift, but in Python I would do something like this:
def remove_children_subviews(view):
view.remove_from_superview()
children = getattr(view, 'subviews', [])
map(remove_children_subviews, children)
I don't see the point in deleting all subviews for child elements, IMHO only the top one is enough, i.e. something like this
[view.subviews makeObjectsPerformSelector: @selector (removeFromSuperview)];
But if you really need everything, then something like that.
-(void) removeSubviews:(UIView*) view{
for (UIView *child in view.subview){
[self removeSubviews:child];
}
[view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question