Quickly remove a subview from a supervisor

I have a scrollView and I added a refreshcontroll to 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 the update method, I have to remove all subviews from scrollview, and then re-fill the scroll.

 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 will get new data, but it no longer has refreshcontroll. I think this is because when I remove subviews from my scrollview, I also remove refreshcontroll from it. (if I add refreshcontroll to my update method again, my scrollview will have refreshconroll again). But there is another problem. After updating my scroll down. I am attached to photos:

This is how I delete the fields:

 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() } 

Here's what scrollview looks like before upgrading: enter image description here

Here's what it looks like after the update: enter image description here

Can someone help me on why this is happening?

Thanks!

Thanks, this solution:

 let subViews = self.scrollView.subviews for subview in subViews{ println("for removing...") if (subview is PostLineItem) { subview.removeFromSuperview() } else { println("not removing..") } } 
+6
source share
2 answers

By deleting all subheadings, you can also remove subheadings other than those that you explicitly added, such as viewing and layout restrictions.

(And to answer your question from the comments, layout restrictions can be essentially a subview. I will explain how to remove all subtasks except layout restrictions here: fooobar.com/questions/978961 / .... )

In general, I recommend changing your code more specifically to remove only the views you added. For example, you can add a tag to the subzones that you add to your addPosts method:

 post.tag = 1000 

then remove only those objects that are in this tag in refresh: ::

 let subViews = self.scrollView.subviews for subview in subViews{ if subview.tag == 1000 { subview.removeFromSuperview() } } 

so that you do not delete any subitems that you have not explicitly added.

Editing: It turns out that the added OP subviews are a custom PostLineItem type, so no tags are needed, since we can just delete all PostLineItem instead:

 for subview in self.view.subviews { if subview is PostLineItem { subview.removeFromSuperview() } } 
+14
source

I also came across this problem and would like to share my solution. You can remove all view sub-items (and their sub-types) with the following UIView extension in swift. In addition, I attached a solution to remove all restrictions in the same way.

 extension UIView { // Recursive remove subviews and constraints func removeSubviews() { self.subviews.forEach({ if !($0 is UILayoutSupport) { $0.removeSubviews() $0.removeFromSuperview() } }) } // Recursive remove subviews and constraints func removeSubviewsAndConstraints() { self.subviews.forEach({ $0.removeSubviewsAndConstraints() $0.removeConstraints($0.constraints) $0.removeFromSuperview() }) } } 

The first solution will contain all the restrictions, see Lyndseys answer. Keep in mind that this solution removes the entire hierarchy below the UIView. If you only want to delete specific subobjects, exchange or expand them as follows:

 if !($0 is UILayoutSupport) && !($0 is PostLineItem) 
+3
source

Source: https://habr.com/ru/post/989095/


All Articles