Drake
Drake

Reputation: 2703

CGPath AddLineToPoint not working

I am trying to draw a diagonal line in my Xamarin.iOS app using the graphics context in my UIView:

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

    using (var gctx = UIGraphics.GetCurrentContext())
    {
        var path = new CGPath();
        path.AddLineToPoint(rect.Width, rect.Size.Height);
        gctx.AddPath(path);
        gctx.SetLineWidth(5);
        gctx.SetFillColor(UIColor.Red.CGColor);
        gctx.FillPath();
        gctx.DrawPath(CGPathDrawingMode.FillStroke);
    }
}

I expect to see a red diagonal line, but I don't see anything?

Upvotes: 0

Views: 102

Answers (1)

Kevin Li
Kevin Li

Reputation: 2258

Solution:

You can achieve that via the code like the below code snippet:

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

        using (var gctx = UIGraphics.GetCurrentContext())
        {
            gctx.SetStrokeColor(UIColor.Red.CGColor);
            gctx.SetLineWidth(2.0f);
            gctx.MoveTo(0, 0);
            gctx.AddLineToPoint(rect.Width, rect.Height);
            gctx.StrokePath();
        }

    }

It works like this:

enter image description here

Upvotes: 1

Related Questions