3

i have several controls (subclasses of UIView) placed on a UIScrollView. Now I want to remove them from the UISrollView and create new controls. The code below runs but has no effect! What am i doing wrong?

        for (int i = 0; i < myScroll.Subviews.Length; i++) {
            Console.WriteLine (myScroll.Subviews [i].GetType ());
            myScroll.Subviews [i].Dispose ();
            myScroll.Subviews [i] = null;
        }
Klaus Grosche
  • 85
  • 1
  • 7

2 Answers2

12

Use RemoveFromSuperview():

foreach(View sub in myScroll.Subviews)
{
   sub.RemoveFromSuperview();
}
Jason
  • 86,222
  • 15
  • 131
  • 146
0

You must remove your control/view only from super view:
If you have those hierarchy of controls

CustomControl <- UIView <- UIScrollView

You can find CustomControl (with help of method ViewWithTag(int tag)) inside of parents UIView or UIScrollView.
BUT: to remove CustomControlsuccessfully, you must call only:

UIView.ViewWithTag(tag).RemoveFromSuperView();

and next line of code will not remove your control:

UIScrollView.ViewWithTag(tag).RemoveFromSuperVew();
rock_walker
  • 453
  • 5
  • 14