如何从我的UIView中仅删除用户添加的子视图 [英] How to remove only user-added subviews from my UIView
问题描述
我正在尝试删除添加到视图中的所有子视图,因此我实现了一个循环,以使用以下内容遍历子视图:
I'm trying to remove all the subviews that I've added to my view, so I've implemented a loop to iterate over the subviews with the following:
for subview in view.subviews {
println(subview)
//subview.removeFromSuperview()
}
我通过在视图中添加UILabel进行了测试,然后运行了这段代码.输出包含我的UILabel,但也包含_UILayoutGuide.因此,我的问题是如何确定子视图是我添加的子视图还是系统添加的子视图?
I tested this by adding a UILabel to my view and then ran this code. The output contained my UILabel but also a _UILayoutGuide. So, my question is how can I determine if a subview is one that I added or one that the system added?
推荐答案
如果只想防止循环删除_UILayoutGuide
(属于类UILayoutSupport
),请尝试以下操作:
If you just want to prevent the loop from removing the _UILayoutGuide
(which is of class UILayoutSupport
), try this:
for subview in self.view.subviews {
if !(subview is UILayoutSupport) {
print(subview)
subview.removeFromSuperview()
}
}
通常来说,如果您想防止删除除_UILayoutGuide
以外的视图,并且您知道要从UIView中删除的子视图的特定类型,则可以限制要删除的子视图这些类型,例如:
And generally speaking, if you'd like to prevent the removal of views other than the _UILayoutGuide
and if you know the specific types of subviews you'd like to remove from your UIView, you can limit the subviews you remove to those types, ex:
for subview in view.subviews {
if subview is ULabel {
println(subview)
subview.removeFromSuperview()
}
}
这篇关于如何从我的UIView中仅删除用户添加的子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!