How to use CustomRenderer for a single ContentPage in Xamarin.iOS and Xamarin.Android

Issue

I need to change the height of the Progress bar but just for a specific ContentPage, not for all my ContentPage’s. I got the code where i change the progress bar height:

public class CustomProgressBarRenderer : ProgressBarRenderer
{
    protected override void OnElementChanged(
    ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
    {
        base.OnElementChanged(e);

        Control.ProgressTintColor  Color.FromRgb(0, 0, 0).ToUIColor();// This changes the color of the progress
    }


    public override void LayoutSubviews()
    {
        base.LayoutSubviews();

        var X  1.0f;
        var Y  10.0f; // This changes the height

        CGAffineTransform transform  CGAffineTransform.MakeScale(X, Y);
        Control.Transform  transform;
    }
}

i’d like to know if it is really possible. Thanks in advance.

Solution

If your App navigation is:

  • Based on Shell

    public class CustomProgressBarRenderer : ProgressBarRenderer
    {
    protected override void OnElementChanged(
    ElementChangedEventArgs e)
    {
    base.OnElementChanged(e);

        if (Shell.Current.CurrentPage.GetType().Name.Equals("YourPageNme"))
        {
             Control.ProgressTintColor  Color.FromRgb(0, 0, 0).ToUIColor();
        }
    }
    
    
    public override void LayoutSubviews()
    {
        base.LayoutSubviews();
    if (Shell.Current.CurrentPage.GetType().Name.Equals("YourPageNme"))
    {
         var X  1.0f;
         var Y  10.0f; // This changes the height
    
         CGAffineTransform transform  CGAffineTransform.MakeScale(X, Y);
         Control.Transform  transform;
    }
    

    }

    }

  • Based on NavigationPage

Replace

   Shell.Current.CurrentPage?.GetType().Name.Equals("YourPageNme")

With

(App.Current.MainPage as NavigationPage)?.CurrentPage?.GetType().Name?.Equals("YourPageNme")

Answered By – Cfun

Leave a Comment