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 

 


Published at

Tags: Xamarin,Xamarin Forms

Luke Alderton

Comments

Kris Adams TV
Excellent! Thanks.
26/03/2017
Pablo
Thanks!!
26/10/2017
Daler
Thanks mate! U made my day! Was wondering where that padding came from ))
03/12/2018
Zal
For those who are confused where to add the line "NavigationPage.SetHasNavigationBar(this, false);" put it in MasterDetailPage.cs. Was confused where to add this ambiguous line of code, then it finally hit me that MasterDetailPage is a NavigationPage. Works like a charm
07/02/2019
Share with
Tags
Latest Comments
By Mark Gentry on Windows Server 2019 - Change product key does nothing
20 Aug 2021, 03:30 AM
By Cathy on In-Place Upgrade for a Windows Server domain controller
31 Jul 2021, 18:28 PM
By Mr. Greymatter on Raspberry Pi - Running Java app on Raspbian
16 Feb 2021, 07:35 AM
By Mikko Seittenranta on Xamarin Forms multiple instances of same app open
16 Feb 2021, 04:34 AM
By Andrew on Auto/Custom height on Xamarin Forms WebView for Android and iOS
22 Jan 2021, 22:15 PM
By Nick on Raspberry Pi - Running Java app on Raspbian
14 Oct 2020, 19:37 PM
By Ivan on Fixed: Value cannot be null Parameter Name: source
15 Sep 2020, 19:47 PM
By Anand on Raspberry Pi - Bluetooth using Bluecove on Raspbian
7 Sep 2020, 16:53 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development