Custom Renderer to Override View Draw on iOS

Hello everyone!

Recently I created a custom renderer for android to draw a graphic Inheriting from View..
On android I simply overwritten the Draw method, and drew everything I need (arcs, circles and text) using canvas.

Now i went to iOS and ran into a problem.
In all the settings I've used and found over the internet, none of them have reached the override Draw method.

Follow the latest code i tried for iOS CustomRenderer:

using CoreGraphics;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(MyProjectName.CustomPieChart), typeof(MyProjectName.iOS.Renderers.CustomPieChartRenderer))]
namespace MyProjectName.iOS.Renderers
{
    class CustomPieChartRenderer : ViewRenderer<CustomPieChart, UIView>
    {
        private int ScaleFactor = 1;

        protected override void OnElementChanged(ElementChangedEventArgs<CustomPieChart> e)
        {
            base.OnElementChanged(e);
            if (Control == null)
            {
                var circleDotView = new UIView();
                SetNativeControl(circleDotView);
            }
        }

        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == CustomPieChart.DataValuesProperty.PropertyName)
            {
                this.SetNeedsDisplay();
            }
        }

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

My custom control is:
public class CustomPieChart : View

My breakpoint never reachs the Draw method.
All my other custom renderes (without draw) are working perfect, those for Android too, including with draw.

Could someone tell me what I'm doing wrong?

Best Answer

  • LeonardoAlmeida.4638LeonardoAlmeida.4638 USUniversity
    Accepted Answer

    I found a simple solution:

    I changed my control base to BoxView instead of View, and my customrenderer reached the Draw.

Answers

Sign In or Register to comment.