Gabriel Juren
Gabriel Juren

Reputation: 31

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

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.

Upvotes: 1

Views: 131

Answers (1)

Cfun
Cfun

Reputation: 9701

If your App navigation is:

public class CustomProgressBarRenderer : ProgressBarRenderer
{
    protected override void OnElementChanged(
    ElementChangedEventArgs<Xamarin.Forms.ProgressBar> 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;
        }
    }
}

Replace

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

With

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

Upvotes: 1

Related Questions