How do I change the height of a Xamarin Forms ProgressBar
in code? Am using Xamarin Forms V2.1.
.HeightRequest
and .MinimumHeightRequest
seem to have no effect. The default progress bar is so thin that it might not even be noticed.
.BackgroundColor
does not seem to work either.
What am I missing here?
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<Xamarin.Forms.ProgressBar> 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<Xamarin.Forms.ProgressBar> 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!
To make the progress bar thicker, you just have to change the ScaleY property of the ProgressBar.
For example: This code
<ProgressBar Progress=".5"/>
<ProgressBar ScaleY="2" Progress=".5"/>
<ProgressBar ScaleY="5" Progress=".5"/>
Produces this
Note that you may need to adjust your margins accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With