How to remove only user objects from my UIView

I'm trying to remove all the subheadings that I added to my view, so I applied a loop to iterate over the subviews with the following:

for subview in view.subviews { println(subview) //subview.removeFromSuperview() } 

I tested this by adding UILabel to my view and then running this code. The result contained my UILabel, but also _UILayoutGuide. So my question is how to determine if a subview is one of them, added by me or added by the system?

+6
source share
4 answers

If you just want the loop to not delete _UILayoutGuide (which has the UILayoutSupport class), try the following:

 for subview in self.view.subviews { if !(subview is UILayoutSupport) { print(subview) subview.removeFromSuperview() } } 

And generally speaking, if you want to prevent deletion of views other than _UILayoutGuide , and if you know the specific types of subheadings that you want to remove from your UIView, you can limit the deleted objects to these types, for example:

 for subview in view.subviews { if subview is ULabel { println(subview) subview.removeFromSuperview() } } 
+13
source

One option is to provide all the views that you add for a specific tag. Then remove them only if they have this tag.

 userCreatedView.tag = 100; 

...

 for subview in view.subviews { if (subview.tag == 100) { subview.removeFromSuperview() } } 

You can also save an array of all subzones added by the user, then check to see if this is a preview in your userAddedViewsArray

Or you can create a subclass of UIView for your users added to it, and then remove only those objects that belong to this class

+9
source

I solve a problem like this way .... first I added a sub view

  • [self.view addSubview: genderPicker];

then remove only this view from the view

  • [genderPicker removeFromSuperview];
+1
source

the indicator is turned off by entering a line speed 2.2 and higher

 func activityonoff(viewcontroler:UIViewController,string:String){ let container: UIView = UIView() let loadingView: UIView = UIView() if string == "on"{ container.frame = viewcontroler.view.frame container.center = viewcontroler.view.center container.backgroundColor = UIColor.whiteColor() container.alpha = 1 container.tag = 1 loadingView.frame = CGRectMake(0, 0, 80, 80) loadingView.center = container.center loadingView.backgroundColor = UIColor(red: 4/255, green: 68/255, blue: 68/255, alpha: 0.7) loadingView.clipsToBounds = true loadingView.layer.cornerRadius = 10 let actInd: UIActivityIndicatorView = UIActivityIndicatorView() actInd.frame = CGRectMake(0.0, 0.0, 40.0, 40.0); actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge actInd.center = CGPointMake(loadingView.frame.size.width / 2,loadingView.frame.size.height / 2); loadingView.addSubview(actInd) container.addSubview(loadingView) viewcontroler.view.addSubview(container) actInd.startAnimating() } else{ for v in viewcontroler.view.subviews{ if v.tag == 1{ v.removeFromSuperview() } } } 

}

0
source

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


All Articles