在 Xamarin.iOS 中设置 UiSwitch 的打开和关闭状态的轨道和拇指颜色

问题描述 投票:0回答:1

我想要一个开关,其打开和关闭状态的颜色相反。 假设在 ON 状态下,拇指颜色 = 蓝色,轨道颜色 = 红色,那么在 OFF 状态下,我希望拇指颜色 = 红色,轨道颜色 = 蓝色。

如何在 xamarin.iOS 中实现此功能。 (不是表格)。 预先感谢。

xamarin.ios uiswitch
1个回答
0
投票

您可以尝试以下代码:

UISwitch uISwitch1 = new UISwitch();
uISwitch1.Frame = new RectangleF(100, 100, 100, 100);
uISwitch1.ThumbTintColor = UIColor.Red;
uISwitch1.Layer.CornerRadius = (nfloat)(uISwitch1.Frame.Height / 2.0);
uISwitch1.BackgroundColor = UIColor.Blue;
uISwitch1.OnTintColor = UIColor.Red;
uISwitch1.ValueChanged += UISwitch1_ValueChanged;

private void UISwitch1_ValueChanged(object sender, EventArgs e)
{
    
    var s = sender as UISwitch;
    bool state = s.On;
    if (s.On)
    {
        s.ThumbTintColor = UIColor.Blue;
    }else
    {
        s.ThumbTintColor = UIColor.Red;
    } 
}

更多信息,您可以参考UISwitch

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.