How to change height of progress bar in Xamarin Forms?

前端 未结 5 436
深忆病人
深忆病人 2021-01-05 11:05

How do I change the height of a Xamarin Forms ProgressBar in code? Am using Xamarin Forms V2.1.

.HeightRequest and .MinimumHeightRequ

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-05 12:00

    You need custom renderers for this:

    First create a class for your custom progress bar:

    public class CustomProgressBar :ProgressBar
    {
      public CustomProgressBar()
       {
       }
    }
    

    And then also add a new file for your custom progress bar renderer:

    For iOS:

    public class CustomProgressBarRenderer : ProgressBarRenderer
    {
        protected override void OnElementChanged(
         ElementChangedEventArgs e)
        {
            base.OnElementChanged(e);
    
            Control.ProgressTintColor = Color.FromRgb(182, 231, 233).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;
        }
    }
    

    [EDIT: fixed code above as per comment]

    For Android:

    public class CustomProgressBarRenderer :ProgressBarRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs e)
        {
            base.OnElementChanged(e);
    
    
            Control.ProgressTintList = Android.Content.Res.ColorStateList.ValueOf(Color.FromRgb(182, 231, 233).ToAndroid()); //Change the color
            Control.ScaleY = 10; //Changes the height
    
        }
    }
    

    I hope this helps you!

提交回复
热议问题