As far as i know, in Xamarin.Forms it is not (yet?) possible to retrieve the TabBar Height, so you might have to go and collect that information per Platform. Then, making use of DependencyService you would be able to make that information available from your Xamarin.Forms shared code. So, lets take a look at how to do it:
iOS
For iOS, there have already been published answers like this to explain how this can be done.
Android
For Android you can retrieve the TabBar height as follows
- Create Interface for Dependency
Note: This interface should also be used on the iOS part ;)
namespace Tabbarheight
{
public interface IDisplayHeights
{
float GetTabBarHeight();
}
}
- Create a class in Android that implements that interface and returns the desired value
(thanks to @LucasZhang-MSFT for the identifier string!)
using Android.App;
using Tabbarheight.Droid;
using Xamarin.Forms;
[assembly: Dependency(typeof(AndroidDisplayHeights))]
namespace Tabbarheight.Droid
{
class AndroidDisplayHeights : IDisplayHeights
{
public static Activity Activity { get; set; }
public float GetTabBarHeight()
{
int resourceId = Activity.Resources.GetIdentifier("design_bottom_navigation_height", "dimen", Activity.PackageName);
int height = 0;
if (resourceId > 0)
{
height = (int)(Activity.Resources.GetDimensionPixelSize(resourceId) / Xamarin.Essentials.DeviceDisplay.MainDisplayInfo.Density);
}
return height;
}
}
}
where Activity is set from MainActivity.cs as follows
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
AndroidDisplayHeights.Activity = this;
LoadApplication(new App());
}
- And finally, you can consume this as follows
public AboutPage()
{
InitializeComponent();
SizeChanged += (s, a) =>
{
label.HeightRequest = DependencyService.Get<IDisplayHeights>().GetTabBarHeight();
label.BackgroundColor = Color.Red;
};
}
In the solution above i defined a Label (label) simply to try to demonstrate the accuracy of the gotten TabBar's Height value by setting the Label's height.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Tabbarheight.Views.AboutPage"
xmlns:vm="clr-namespace:Tabbarheight.ViewModels"
Title="{Binding Title}">
<StackLayout>
<Label x:Name="label">
<Label.Text>
<x:String>
Hola mundo
</x:String>
</Label.Text>
</Label>
</StackLayout>
</ContentPage>
The label looks like this on my side:
