ios Swift 从超级视图中删除子视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30831444/
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
Swift remove subviews from superview
提问by solarenqu
i have a scrollView, and i added a refreshcontroll to it.
我有一个scrollView,我给它添加了一个refreshcontroll。
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Frissítéshez húzzad! :)")
self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
self.scrollView.addSubview(refreshControl)
in the refresh method i have to remove all subviews from the scrollview, then repopulate the scrollview..
在刷新方法中,我必须从滚动视图中删除所有子视图,然后重新填充滚动视图。
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Frissítéshez húzzad! :)")
self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
self.scrollView.addSubview(refreshControl)
after i try to pull, my scrollview get new data but it's don't have anymore refreshcontroll. i think it is because when i removing subviews from my scrollview i also remove the refreshcontroll from it. (if i add the refreshcontroll again in my refresh method my scrollview will have refreshconroll again) But there is another problem. After refreshing my scrollview moving down.. i attached to pictures:
在我尝试拉动后,我的滚动视图获得了新数据,但它不再具有刷新控制。我认为这是因为当我从滚动视图中删除子视图时,我也从中删除了 refreshcontroll。(如果我在我的刷新方法中再次添加 refreshcontroll,我的滚动视图将再次有 refreshconroll)但是还有另一个问题。刷新我的滚动视图向下移动后..我附上了图片:
This is how i remove the subiews:
这就是我删除子视图的方式:
func refresh(sender:AnyObject)
{
//remove all subviews from scrollview..
let subViews = self.scrollView.subviews
for subview in subViews{
println("for removing...")
subview.removeFromSuperview()
}
println("refresh called..")
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
//remove all elements from the array
tstFrames.removeAll(keepCapacity: false)
//refresh data from webservice and adding it to tstFrames Array
wsServiceFeedTst()
//adding items to the scrollview from tstFramesArray
addPosts()
self.refreshControl.endRefreshing()
}
This is how scrollview look like before refreshing:
这是刷新之前滚动视图的样子:
An this is how it looks like after refreshing:
这是刷新后的样子:
could anybody help me about why is this moving down?
有人可以帮助我了解为什么会下降吗?
Thank you!
谢谢!
Thank you, this is the solution:
谢谢,解决方法如下:
let subViews = self.scrollView.subviews
for subview in subViews{
println("for removing...")
if (subview is PostLineItem) {
subview.removeFromSuperview()
}
else {
println("not removing..")
}
}
回答by Lyndsey Scott
By removing all subviews, you can also be removing subviews other than the ones you explicitly added, like the refresh view and layout constraints.
通过删除所有子视图,您还可以删除显式添加的子视图以外的子视图,例如刷新视图和布局约束。
(And to answer your question from the comments, layout constraints can in fact be subviews. I explain how to remove all subviews except layout constraints here: https://stackoverflow.com/a/27281242/2274694.)
(为了从评论中回答您的问题,布局约束实际上可以是子视图。我在这里解释了如何删除除布局约束之外的所有子视图:https: //stackoverflow.com/a/27281242/2274694。)
In general, I recommend changing your code to be more specific such that only the views you've added are removed. For example, you could add a tag to the subviews you add in your addPosts
method:
通常,我建议将您的代码更改为更具体,以便仅删除您添加的视图。例如,您可以在addPosts
方法中添加的子视图中添加一个标签:
post.tag = 1000
then remove only the subviews with that tag in refresh:
:
然后只删除带有该标签的子视图refresh:
:
let subViews = self.scrollView.subviews
for subview in subViews{
if subview.tag == 1000 {
subview.removeFromSuperview()
}
}
to ensure that you don't remove any subviews you haven't explicitly added yourself.
以确保您不会删除您自己未明确添加的任何子视图。
Edit: It turns out the OP's added subviews are all of custom type PostLineItem
so the tags are unnecessary since we can just remove all the PostLineItem
s instead:
编辑:原来 OP 添加的子视图都是自定义类型,PostLineItem
所以标签是不必要的,因为我们可以删除所有的PostLineItem
s 代替:
for subview in self.view.subviews {
if subview is PostLineItem {
subview.removeFromSuperview()
}
}
回答by masp
I stumbled about this problem today as well and would like to share my solution. You can remove all subviews of a view (and their subviews) with the following UIView extension in swift. In addition I attached a solution for removing all constraints the same way.
我今天也偶然发现了这个问题,想分享我的解决方案。您可以使用以下 UIView 扩展快速删除视图(及其子视图)的所有子视图。此外,我附上了一个以相同方式删除所有约束的解决方案。
extension UIView {
// Recursive remove subviews and constraints
func removeSubviews() {
self.subviews.forEach({
if !(if !(##代码## is UILayoutSupport) && !(##代码## is PostLineItem)
is UILayoutSupport) {
##代码##.removeSubviews()
##代码##.removeFromSuperview()
}
})
}
// Recursive remove subviews and constraints
func removeSubviewsAndConstraints() {
self.subviews.forEach({
##代码##.removeSubviewsAndConstraints()
##代码##.removeConstraints(##代码##.constraints)
##代码##.removeFromSuperview()
})
}
}
The first solution will keep all contraints, see Lyndseys answer. Be aware that this solution removes the whole hierarchy below the UIView. If you only want to remove specific subviews exchange or extend like this:
第一个解决方案将保留所有限制条件,请参阅 Lyndseys 的回答。请注意,此解决方案删除了 UIView 下方的整个层次结构。如果您只想删除特定的子视图交换或像这样扩展:
##代码##