ios UIStackView :是否真的需要同时调用 removeFromSuperView 和 removeArrangedSubview 来删除子视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37525706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIStackView : Is it really necessary to call both removeFromSuperView and removeArrangedSubview to remove a subview?
提问by robola
From the UIStackView Class Reference
In removeArrangedSubview:
在 removeArrangedSubview 中:
To prevent the view from appearing on screen after calling the stack's removeArrangedSubview: method, explicitly remove the view from the subviews array by calling the view's removeFromSuperview method.
为了防止在调用堆栈的 removeArrangedSubview: 方法后视图出现在屏幕上,请通过调用视图的 removeFromSuperview 方法从子视图数组中显式删除视图。
In arrangedSubview:
在安排的子视图中:
Whenever an arranged view's removeFromSuperview method is called, the stack view removes the view from its arrangedSubview array
每当调用排列视图的 removeFromSuperview 方法时,堆栈视图都会从其排列的子视图数组中删除该视图
From these, it seems that calling just removeFromSuperview is enough to remove a subview and I've been using it like that without problems. I also confirmed the behavior by logging the count of the arrangedSubviews array when removeFromSuperview is called.
从这些来看,似乎只调用 removeFromSuperview 就足以删除子视图,而且我一直这样使用它,没有问题。我还通过在调用 removeFromSuperview 时记录排列的子视图数组的计数来确认该行为。
A lot of tutorials and comments here on S/O however, say to call both. Is there a reason for this? Or do people just do it because the documentation says so?
然而,这里有很多关于 S/O 的教程和评论,都说要调用两者。是否有一个原因?还是人们只是因为文档这样说才这样做?
回答by zurakach
No, just call subview.removeFromSuperview()
不,只是打电话 subview.removeFromSuperview()
/* Removes a subview from the list of arranged subviews without removing it as a subview of the receiver. To remove the view as a subview, send it -removeFromSuperview as usual; the relevant UIStackView will remove it from its arrangedSubviews list automatically. */ open func removeArrangedSubview(_ view: UIView)
/* Removes a subview from the list of arranged subviews without removing it as a subview of the receiver. To remove the view as a subview, send it -removeFromSuperview as usual; the relevant UIStackView will remove it from its arrangedSubviews list automatically. */ open func removeArrangedSubview(_ view: UIView)
回答by Changnam Hong
In iOS 12.0, You need to use
在 iOS 12.0 中,您需要使用
stackView.arrangedSubviews[index].removeFromSuperview()
If you use removeArrangedSubview
, there is a bug where the view at the specified index removed, but the view I want to clear appears at CGPoint(x: 0, y: 0)
.
如果使用removeArrangedSubview
,则存在一个错误,即删除了指定索引处的视图,但我要清除的视图出现在CGPoint(x: 0, y: 0)
.
Hope this help someone.
希望这有助于某人。
回答by inreflection7
Hacking With Swift provides a pretty good example and explanation, using Web views in this case.
Hacking With Swift 提供了一个很好的例子和解释,在这种情况下使用 Web 视图。
The reason is that you can remove something from a stack view's arranged subview list then re-add it later, without having to recreate it each time – it was hidden, not destroyed. We don't want a memory leak, so we want to remove deleted web views entirely. If you find your memory usage ballooning, you probably forgot this step!
原因是您可以从堆栈视图的排列子视图列表中删除某些内容,然后稍后重新添加它,而不必每次都重新创建它——它被隐藏了,而不是被销毁。我们不希望内存泄漏,因此我们希望完全删除已删除的 Web 视图。如果您发现内存使用量激增,您可能忘记了这一步!
回答by Graham Perks
You're right, just the call to removeFromSuperview
is sufficient to have the view fully removed.
您是对的,只需调用removeFromSuperview
就足以完全删除视图。
I suspect the reason for people putting both is because they run across the removeArrangedSubview
documentation which seems to say both are needed. (And indeed, they are, if you call removeArrangedSubview
and want the view really gone.)
我怀疑人们放置两者的原因是因为他们遇到了removeArrangedSubview
似乎说两者都需要的文档。(事实上,如果你打电话removeArrangedSubview
并希望视图真的消失的话,它们是。)
The additional doc in arrangedSubviews
is not seen by so many, so they don't realize removeArrangedSubview
is, in this case, optional.
arrangedSubviews
很多人都没有看到额外的文档,所以他们没有意识到removeArrangedSubview
在这种情况下是可选的。
回答by Ajumal
To remove a arrangedSubview from a stackview is
从堆栈视图中删除排列的子视图是
// To remove it from the view hierarchy – this is important!
subView.removeFromSuperview()
回答by Nik Kov
For removing i use this extension. Don't really know is it necessary to remove constraints. But maybe it would help.
为了删除我使用这个扩展。不知道是否有必要删除约束。但也许它会有所帮助。
extension UIStackView {
func removeAllArrangedSubviews() {
let removedSubviews = arrangedSubviews.reduce([]) { (allSubviews, subview) -> [UIView] in
self.removeArrangedSubview(subview)
return allSubviews + [subview]
}
for v in removedSubviews {
if v.superview != nil {
NSLayoutConstraint.deactivate(v.constraints)
v.removeFromSuperview()
}
}
}
}
回答by Anit Kumar
No, To remove specific view.
不,要删除特定视图。
func removeArrangedSubview(_ view: UIView)
func removeArrangedSubview(_ view: UIView)
To remove all subviews
删除所有子视图
stackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
stackView.arrangedSubviews.forEach { $0.removeFromSuperview() }