Xamarin MasterDetailPage has large margin/padding at top

Xamarin MasterDetailPage with content.When creating an app the uses a MasterDetailPage as the root Page, you might sometimes find that you've got a large set of padding at the top of all your pages with seemingly no way to remove it.

Okay so this is what you're currently doing or something similar... In your app page, you have something like this:

Application.Current.MainPage = new NavigationPage(new MainPage());

And in your MasterDetailPage within the onSelected handler of the menu, you set your pages to also use NavigationPage like this:

Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));

That works fine; it loads new pages and seems to flow correctly. Until...

What's that space at the top coming from? Is that padding or what?

So you try setting it to hide the navigation bar using this:

NavigationPage.SetHasNavigationBar(this, false);

But that doesn't solve your issue, what's going on?

So it all comes back to the first line of code we mentioned, Your MasterDetailPage should not be wrapped with a NavigationPage when you set it as the MainPage, it should look like this:

Application.Current.MainPage = new MainPage();


Also, make sure you remove any line you find that removes the navigation bar for content pages, like the one above that uses SetHasNavigationBar, as this will hide the bar you do want.

Now your issue should be solved. Congrats. It'll now look something like the image on the right. As you can see there is no longer any padding at the top pushing all the content down.

For some useful example code check this example project out, it might not run as it's a fairly old project and the NuGet packages are out of date, but the example code is still very relevant and works well: https://developer.xamarin.com/samples/xamarin-forms/Navigation/MasterDetailPage/

Edit: For more information, see this useful post on the Xamarin forums: https://forums.xamarin.com/discussion/comment/185322/#Comment_185322 

 


By Luke Alderton at 24 Nov 2016, 09:39 AM

Tags: Xamarin,Xamarin Forms

Comments

Kris Adams TV
Excellent! Thanks.
26/03/2017
Pablo
Thanks!!
26/10/2017
Post a comment
Sit tight...
We're adding your comment.
Thank you.
Your comment has been added.
There's been a problem.
Please try again later.
Share with
Tags
Latest Comments
By dipbleds on Raspberry Pi - Bluetooth using Bluecove on Raspbian
12 Dec 2017, 21:29 PM
By Ich on Replacing the Xamarin Header/Navigation bar with a custom view/template
26 Nov 2017, 17:11 PM
By Ravi Motha on My experience at Umbraco UK Festival 2017
12 Dec 2017, 08:39 AM
By Borges on How to add a Xamarin Forms Loading Screen/Overlay
7 Nov 2017, 19:11 PM
By Rutul Mehta on Auto/Custom height on Xamarin Forms WebView for Android and iOS
4 Nov 2017, 03:15 AM
By Pablo on Xamarin MasterDetailPage has large margin/padding at top
26 Oct 2017, 15:31 PM
By faiza on Using MaryTTS or OpenMary in Java
22 Oct 2017, 12:54 PM
By Nietoperz on Using MaryTTS or OpenMary in Java
29 Sep 2017, 14:34 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development