Remove all subviews?

2020-01-25 03:07发布

When my app gets back to its root view controller, in the viewDidAppear: method I need to remove all subviews.

How can I do this?

标签: ios subview
15条回答
Luminary・发光体
2楼-- · 2020-01-25 03:51

In objective-C, go ahead and create a category method off of the UIView class.

- (void)removeAllSubviews
{
    for (UIView *subview in self.subviews)
        [subview removeFromSuperview];
}
查看更多
倾城 Initia
3楼-- · 2020-01-25 03:52

In monotouch / xamarin.ios this worked for me:

SomeParentUiView.Subviews.All(x => x.RemoveFromSuperview);
查看更多
三岁会撩人
4楼-- · 2020-01-25 03:57

If you're using Swift, it's as simple as:

subviews.map { $0.removeFromSuperview }

It's similar in philosophy to the makeObjectsPerformSelector approach, however with a little more type safety.

查看更多
登录 后发表回答