Computer Magic Logo
Fix iOS single page problem

Sunday, April 10, 2016

Published by Aristotelis Pitaridis

The iOS version of a Xamarin single page has a problem. The status bar at the top of the screen covers area of the page. This problem does not exist in a multipage-navigation application. In order to fix this problem for a single page we can change the padding of the page only for the iOS version of the application. The problem is that we need to set the top padding only for the iOS version of the application and this is the reason that we will use the Device.OnPlatform member function which allows us to get a different value depending on the operating system the application is executed.

public partial class Page1 : ContentPage
{
    public Page1 ()
    {
        InitializeComponent ();

        Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0);
    }
}

Something else that we could do is to set the padding only for the iOS application using the __IOS__ preprocessor directive. This option is available only when we have a Xamarin.Forms shared project.

public partial class Page1 : ContentPage
{
    public Page1 ()
    {
        InitializeComponent ();

#if __IOS__ 
        Padding = new Thickness(0, 20, 0, 0); 
#endif 
    }
}