0

The default size of an Xamarin Switch is too big, so I wanted to make it smaller. I tried setting HeightRequest = 20, but it doesn't work on any platform, like UWP, iOS.

Is there a way to change the Switch Size (height)?

Thanks!

James H.

renintx
  • 15
  • 3

1 Answers1

0

You may use Xamarin.Forms Custom Renderers to customize the appearance and behavior of Xamarin.Forms controls.

Try the following steps:

1.Create a Xamarin.Forms custom CustomSwitch subclassing the Switch control

public class CustomSwitch : Switch
{
    public CustomSwitch()
    {
    }
}

2.Consume the custom control from Xamarin.Forms.

<StackLayout>
    ...
    <local:CustomSwitch IsToggled="true"/>
</StackLayout>

3.Creating the Custom Renderer on iOS. You could use CGAffineTransform.MakeScale like @FabriBertani said in the comments.

[assembly: ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))]
namespace MyProject.iOS

public class CustomSwitchRenderer : SwitchRenderer
{
    public CustomSwitchRenderer()
    {
    }


    protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {

            //Control.Transform = CGAffineTransform.MakeScale(0.5f, 0.5f);
            Control.Transform = CGAffineTransform.MakeScale(2f, 2f);
        }
    }
}

For more info, you could refer to Customizing an Entry , CGAffineTransform.MakeScale(nfloat, nfloat) Method.

Hope it works!

Liqun Shen-MSFT
  • 3,490
  • 2
  • 3
  • 11