sylmz sylmz - 3 months ago 63
iOS Question

Xamarin forms toggle button default color on IOS

How could I change the toggle button default color which is green on IOS?

Answer

Create a custom Render for the switch (UISwitch in iOS) and set its OnTintColor Property.

In PCL :

public class CustomSwitch : Switch
{
}

In iOS :

[assembly: ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))]

namespace xyz.iOS.CustomControlRenderers
{
    public class CustomSwitchRenderer : SwitchRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs<Switch> e)
        {
            base.OnElementChanged (e);

            if (Control != null) 
            {
                // do whatever you want to the UISwitch here!
                Control.OnTintColor = UIColor.FromRGB (204, 153, 255);
            }
         }
    }
}

Since you don't need any customizations for android you need not create a custom render in the android platform.

You can get the list of all the native renders corresponding to Forms implementation from here. An example of custom render can be found here.

Comments