title: Programmatically draw rectangles with MonoTouch
body: <p>I am trying to learn more about MonoTouch and so I tried out drawing with Quartz2D. I want to create an example, where I draw n-rectangles programmatically. The problem is that only the first one is drawn. I think that the second one is deleted/cleared/covered by the first one.</p>
  
  <p>Here is my code:</p>
  
  <p>SingleViewMTViewController.cs</p>
  
  <pre><code>public override void ViewDidLoad ()
      {
          base.ViewDidLoad ();
  
          PointF locationOne = new PointF (5, 5);
          PointF locationTwo = new PointF (5, 100);
          SizeF size = new SizeF (120, 40);
  
          DraggableRectangle tView = new DraggableRectangle (locationOne, size, UIColor.Yellow);
          DraggableRectangle tView2 = new DraggableRectangle (locationTwo, size, UIColor.Brown);
  
          DraggableRectangle[] views = new DraggableRectangle[2];
          views [0] = tView;
          views [1] = tView2;
  
          View.AddSubviews (views);
      }
  </code></pre>
  
  <p>DraggableRectangle.cs</p>
  
  <pre><code>public class DraggableRectangle : UIView
  {
      private CGPath path;
      private PointF _targetLocation;
      private SizeF _size;
      private UIColor _fillColor = UIColor.Brown;
  
      public DraggableRectangle (PointF targetLocation, SizeF size)
      {
          _targetLocation = targetLocation;
          _size = size;
  
          RectangleF frameRect = new RectangleF (_targetLocation.X, _targetLocation.Y, _size.Width, _size.Height);
          this.Frame = frameRect;
          this.BackgroundColor = UIColor.Clear; //without this, nothing is drawn
      }
  
      public DraggableRectangle (PointF targetLocation, SizeF size, UIColor fillColor):this(targetLocation,size)
      {
          _fillColor = fillColor;
      }
  
      public override void Draw (RectangleF rect)
      {
          //base.Draw (rect);
          //works without base-call?
  
          //get graphics context
          using (CGContext gctx = UIGraphics.GetCurrentContext ()) {
  
              //set up drawing attributes
              _fillColor.SetFill ();
  
              //create geometry
              path = new CGPath ();
  
              path.AddRect (new RectangleF (_targetLocation.X, _targetLocation.Y, _size.Width, _size.Height));
  
              path.CloseSubpath ();
  
              //add geometry to graphics context and draw it
              gctx.AddPath (path);        
              gctx.DrawPath (CGPathDrawingMode.FillStroke);   
          }       
      }
  </code></pre>
  
  <p>Is there a better way to draw independent rectangles with MonoTouch? Or could please someone explain, what I am doing wrong?</p>
  
  <p><strong>UPDATE:</strong> Thats btw. the best output I can achieve, but thats just not correct for "Yellow" and "Brown"
  <a href="http://www.bilder-upload.eu/show.php?file=81bfea-1329755225.png" rel="nofollow noreferrer">http://www.bilder-upload.eu/show.php?file=81bfea-1329755225.png</a>
  <a href="http://www.bilder-upload.eu/show.php?file=81bfea-1329755225.png" rel="nofollow noreferrer">http://www.bilder-upload.eu/show.php?file=81bfea-1329755225.png</a></p>