UIView.Draw(CGRect) Method

Definition

Draws the view within the passed-in rectangle.

[Foundation.Export("drawRect:")]
[ObjCRuntime.ThreadSafe]
public virtual void Draw (CoreGraphics.CGRect rect);
abstract member Draw : CoreGraphics.CGRect -> unit
override this.Draw : CoreGraphics.CGRect -> unit

Parameters

rect
CGRect

The RectangleF to draw.

Attributes

Remarks

The Draw(CGRect) method should never be called directly. It is called by iOS during run loop processing. The first time through the run loop, it is called. After that, it will be called on demand whenever the view has been marked as needing display by calling SetNeedsDisplayInRect(CGRect) or SetNeedsDisplayInRect(CGRect).

Core Graphics uses device independent points rather than pixels. This allows drawing code to scale between different resolutions. For example, on a Retina display, 1 point is equivalent to 2 pixels, while on non-Retina displays, 1 point corresponds to 1 pixel.

public override void Draw (RectangleF rect)
{
    base.Draw (rect);

    var context = UIGraphics.GetCurrentContext ();

    context.SetLineWidth(4);
    UIColor.Red.SetFill ();
    UIColor.Blue.SetStroke ();

    var path = new CGPath ();

    path.AddLines(new PointF[]{
    new PointF(100,200),
    new PointF(160,100), 
    new PointF(220,200)});

    path.CloseSubpath();

    context.AddPath(path);		
    context.DrawPath(CGPathDrawingMode.FillStroke);
}

This can be used from a background thread.

Applies to