iosxamarin.iosuiswitch

Color in UISwitch track


I have a UISwitch to which I am customising the thumb and track color.

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

If the background color is Black, it shows black in the track like in pic Black-Background

But if the background color is white as in

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

It shows gray colour track instead of white as in

White-Background.

I am not understanding why the white color is not visible. It works fine with any other color. Also, the same white color shows fine with the thumb in the "On" state. (theme.ColorInput is white color, "#ffffff")


Solution

  • If you refer to Apple official document about UISwitch, you may find there's no Property for us to change tint color of UISwitch in OFF state. By default is now light grey.

    The easiest way to truly change the tint color in OFF state of UISwitch is to change the color of the subviews of the UISwitch,

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

    This way goes into the UISwitch's subview, and change its background color.

    enter image description here

    Hope it helps!