Alan2
Alan2

Reputation: 24572

Is there a way to change the outline color of an iOS switch in Xamarin with a renderer?

I am using this class and renderer which allows me to change the color of my switch:

public class ExtSwitch : Switch
{
    public static readonly BindableProperty SwitchOnColorProperty = BindableProperty.Create(nameof(SwitchOnColor), typeof(Color), typeof(ExtSwitch), Color.Default);
    public static readonly BindableProperty SwitchOffColorProperty = BindableProperty.Create(nameof(SwitchOffColor), typeof(Color), typeof(ExtSwitch), Color.Default);
    public static readonly BindableProperty SwitchThumbColorProperty = BindableProperty.Create(nameof(SwitchThumbColor), typeof(Color), typeof(ExtSwitch), Color.Default);

    public Color SwitchOffColor { get => (Color)GetValue(SwitchOffColorProperty); set => SetValue(SwitchOffColorProperty, value); }
    public Color SwitchOnColor { get => (Color)GetValue(SwitchOnColorProperty); set => SetValue(SwitchOnColorProperty, value); }
    public Color SwitchThumbColor { get => (Color)GetValue(SwitchThumbColorProperty); set => SetValue(SwitchThumbColorProperty, value); }

}

and

class ExtSwitchRenderer : SwitchRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null || e.NewElement == null) return;

        ExtSwitch s = Element as ExtSwitch;

        //this.Control.ThumbTintColor = s.SwitchThumbColor.ToUIColor();
        this.Control.OnTintColor = s.SwitchOnColor.ToUIColor();
    }
}

This changes the color but not the outline border to the switch. Here in this image it appears in white:

enter image description here

Is there a way that I could change the white border to another color in iOS?

Upvotes: 0

Views: 322

Answers (1)

Bruno Caceiro
Bruno Caceiro

Reputation: 7189

You should change the TintColor

this.Control.TintColor = s.SwitchOnColor.ToUIColor();

Upvotes: 1

Related Questions