ios - Swift remove subviews from superview -


i have scrollview, , added refreshcontroll it.

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 refresh method have remove subviews scrollview, repopulate 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 try pull, scrollview new data it's don't have anymore refreshcontroll. think because when removing subviews scrollview remove refreshcontroll it. (if add refreshcontroll again in refresh method scrollview have refreshconroll again) there problem. after refreshing scrollview moving down.. attached pictures:

this how remove subiews:

func refresh(sender:anyobject) {     //remove subviews scrollview..     let subviews = self.scrollview.subviews     subview in subviews{         println("for removing...")         subview.removefromsuperview()     }      println("refresh called..")     uiapplication.sharedapplication().applicationiconbadgenumber = 0     //remove elements array     tstframes.removeall(keepcapacity: false)     //refresh data webservice , adding tstframes array     wsservicefeedtst()     //adding items scrollview tstframesarray     addposts()     self.refreshcontrol.endrefreshing()  } 

this how scrollview before refreshing: enter image description here

an how looks after refreshing: enter image description here

could me why moving down?

thank you!

thank you, solution:

let subviews = self.scrollview.subviews subview in subviews{     println("for removing...")     if (subview postlineitem) {         subview.removefromsuperview()     }     else {         println("not removing..")     } } 

by removing subviews, can removing subviews other ones explicitly added, refresh view , layout constraints.

(and answer question comments, layout constraints can in fact subviews. explain how remove subviews except layout constraints here: https://stackoverflow.com/a/27281242/2274694.)

in general, recommend changing code more specific such views you've added removed. example, add tag subviews add in addposts method:

post.tag = 1000 

then remove subviews tag in refresh::

let subviews = self.scrollview.subviews subview in subviews{     if subview.tag == 1000 {         subview.removefromsuperview()     } } 

to ensure don't remove subviews haven't explicitly added yourself.

edit: turns out op's added subviews of custom type postlineitem tags unnecessary since can remove postlineitems instead:

for subview in self.view.subviews {     if subview postlineitem {         subview.removefromsuperview()      } } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -