I used Drawable
to customize the rendering of the ProgressBar
in Android
as answered to this question but the solution is not working with iOS.
Below is how it renders in Android
.
Below is how it renders in iOS
Below is the code for my iOS
CustomRenderer
[assembly: ExportRenderer(typeof(CustomProgressbar), typeof(CustomProgressBarRenderer))]
namespace Demo.iOS.Renderers
{
public class CustomProgressBarRenderer : ProgressBarRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<ProgressBar> e)
{
try
{
base.OnElementChanged(e);
if (Control != null)
{
Control.ProgressTintColor = Color.FromHex("#ff0000").ToUIColor();
Control.TrackTintColor = Color.FromHex("#3489cc").ToUIColor();
}
}
catch (Exception ex)
{
}
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
var X = 1.0f;
var Y = 15.0f;
CGAffineTransform _transform = CGAffineTransform.MakeScale(X, Y);
this.Transform = _transform;
this.ClipsToBounds = true;
this.Layer.MasksToBounds = true;
this.Layer.CornerRadius = 5;
}
}
}
How do I accomplish this?
According to @SushiHangover's answer, we can make a ViewRenderer
to achieve your effect.
Firstly, create our own ProgressView
, make sure it is inherited from ContentView
. also add a BindableProperty
to present the value:
public partial class ProgressView : ContentView
{
public double Progress
{
set { SetValue(ProgressProperty, value); }
get { return (double)GetValue(ProgressProperty); }
}
public readonly static BindableProperty ProgressProperty = BindableProperty.Create("Progress", typeof(double), typeof(ProgressView), 0.0);
...
}
Then, We can make the Custom renderer like:
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
//You can refer to @SushiHangover's method for detail's code, here I use the same name.
Setup();
Complete = ((ProgressView)Element).Progress;
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == "Progress")
{
Complete = ((ProgressView)Element).Progress;
}
}
Because this is in renderer, we should refresh our label's frame:
public override void Draw(CGRect rect)
{
base.Draw(rect);
...
label.Frame = Bounds;
}
At last we can use it on Forms like:
<local:ProgressView x:Name="MyProgress" HeightRequest="50"/>
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