1
votes

I am working on a project where i need to get the navigation bar height.I am getting the whole screen height using:

Helpers.ApplicationContext.ScreenHeight = (int)Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density;
Helpers.ApplicationContext.ScreenWidth = (int)Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density;

But I am not able to get the navigation bar height.

Can any one please suggest an idea to get the navigation bar height of my page.

Thanks in advance.

1

1 Answers

1
votes

For iOS This may help you :

var navbarHeight = YourViewControllerInstance.NavigationController?.NavigationBar.Frame.Height;

For Android :

TypedArray styledAttributes = this.Theme.ObtainStyledAttributes(
new int[] { Android.Resource.Attribute.ActionBarSize });
var actionbarHeight = (int) styledAttributes.GetDimension(0, 0);
styledAttributes.recycle();

Above example gives Height in Pixel. Please find more refined sample as below which provide height in Pixel as well as DP:

TypedValue tv = new TypedValue();
int actionBarHeightInPixel = 0;
int actionBarHeightInDP = 0;
DisplayMetrics metrics = Resources.DisplayMetrics;

if (Theme.ResolveAttribute(Resource.Attribute.actionBarSize, tv, true))
{
    actionBarHeightInPixel = TypedValue.ComplexToDimensionPixelSize(tv.Data, metrics);
    actionBarHeightInDP = actionBarHeightInPixel / (int)metrics.Density;
}