How do I test if a subview has already been added to a parent view? If it hasn't been added, I want to add it. Otherwise, I want to remove it.
Check if a subview is in a view using Swift
50.1k views Asked by Suragch At
        	4
        	
        There are 4 answers
0
                 On
                        
                            
                        
                        
                            On
                            
                            
                                                    
                    
                Here we used two different views. Parent view is the view in which we are searching for descendant view and check wether added to parent view or not.
if parentView.subviews.contains(descendantView) {
   // descendant view added to the parent view.
  }else{
   // descendant view not added to the parent view.
}
You can use the
UIViewmethodisDescendantOfView:You may also need to surround everything with
if mySubview != nildepending on your implementation.