我有一个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)
在刷新方法中,我必须从scrollview中删除所有子视图,然后重新填充scrollview ..
self.refreshControl = UIRefreshControl() self.refreshControl.attributedTitle = NSAttributedString(string: "Frissítéshez húzzad! :)") self.refreshControl.addTarget(self,forControlEvents: UIControlEvents.ValueChanged) self.scrollView.addSubview(refreshControl)
在我尝试拉动之后,我的scrollview获得了新的数据,但它不再具有refreshcontroll.我认为这是因为当我从我的scrollview中删除子视图时,我也从中删除了refreshcontroll. (如果我在刷新方法中再次添加refreshcontroll,我的scrollview将再次刷新卷轴)但是还有另一个问题.刷新我的滚动视图后向下移动..我附在图片上:
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() }
这是刷新视图在刷新之前的样子:
这是刷新后的样子:
任何人都可以帮我解释为什么会这样下降?
谢谢!
谢谢,这是解决方案:
let subViews = self.scrollView.subviews for subview in subViews{ println("for removing...") if (subview is PostLineItem) { subview.removeFromSuperview() } else { println("not removing..") } }
解决方法
通过删除所有子视图,您还可以删除明确添加的子视图以外的子视图,例如刷新视图和布局约束.
(并且从评论中回答你的问题,布局约束实际上可以是子视图.我解释了如何删除除布局约束之外的所有子视图:https://stackoverflow.com/a/27281242/2274694.)
通常,我建议您将代码更改为更具体,以便仅删除您添加的视图.例如,您可以在addPosts方法中添加的子视图中添加标记:
post.tag = 1000
let subViews = self.scrollView.subviews for subview in subViews{ if subview.tag == 1000 { subview.removeFromSuperview() } }
编辑:事实证明,OP添加的子视图都是自定义类型PostLineItem,因此标签是不必要的,因为我们可以删除所有的PostLineItems:
for subview in self.view.subviews { if subview is PostLineItem { subview.removeFromSuperview() } }