UISwitch 轨道中的颜色

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

我有一个 UISwitch,我正在自定义拇指和轨道颜色。

if (locationTrackingSwitch.On)
    locationTrackingSwitch.ThumbTintColor = FromHEX(theme.colorInput);
else
    locationTrackingSwitch.ThumbTintColor = FromHEX(theme.colorButton);
locationTrackingSwitch.Layer.CornerRadius = (nfloat)(locationTrackingSwitch.Frame.Height / 2.0);
locationTrackingSwitch.BackgroundColor = UIColor.Black; //FromHEX(theme.colorInput);
locationTrackingSwitch.OnTintColor = FromHEX(theme.colorButton);

如果背景颜色是黑色,则轨道中显示黑色,如图所示

但是如果背景颜色是白色

locationTrackingSwitch.BackgroundColor = UIColor.White; 
//FromHEX(theme.colorInput);` 

它显示灰色轨道而不是白色轨道

.

我不明白为什么白色不可见。它与任何其他颜色都可以很好地搭配。此外,在“打开”状态下,拇指也可以显示相同的白色。 (theme.ColorInput 是白色,“#ffffff”)

ios xamarin.ios uiswitch
1个回答
0
投票

如果你参考Apple关于UISwitch的官方文档,你可能会发现没有属性可以让我们在OFF状态下改变UISwitch的色调颜色。现在默认为浅灰色。

真正更改 UISwitch OFF 状态下的色调颜色的最简单方法是更改 UISwitch 子视图的颜色,

locationTrackingSwitch.subviews[0].subviews[0].backgroundColor = UIColor.White;

这种方式进入UISwitch的子视图,并更改其背景颜色。

希望有帮助!

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