首页 > 解决方案 > 有没有办法使用渲染器更改 Xamarin 中 iOS 开关的轮廓颜色?

问题描述

我正在使用此类和渲染器,它允许我更改开关的颜色:

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); }

}

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();
    }
}

这会更改颜色,但不会更改开关的轮廓边框。在此图像中,它显示为白色:

在此处输入图像描述

有没有办法可以在iOS中将白色边框更改为另一种颜色?

标签: xamarinxamarin.forms

解决方案


你应该改变 TintColor

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

推荐阅读