Programmatically Get Height of Navigation Bar

Programmatically get height of navigation bar

The light bulb started to come on. Unfortunately, I have not discovered a uniform way to correct the problem, as described below.

I believe that my whole problem centers on my autoresizingMasks. And the reason I have concluded that is the same symptoms exist, with or without a UIWebView. And that symptom is that everything is peachy for Portrait. For Landscape, the bottom-most UIButton pops down behind the TabBar.

For example, on one UIView, I have, from top to bottom:

UIView – both springs set (default case) and no struts

UIScrollView -
If I set the two springs, and clear everything else (like the UIView), then the UIButton intrudes on the object immediately above it.
If I clear everything, then UIButton is OK, but the stuff at the very top hides behind the StatusBar
Setting only the top strut, the UIButton pops down behind the Tab Bar.

UILabel and UIImage next vertically – top strut set, flexible everywhere else

Just to complete the picture for the few that have a UIWebView:

UIWebView -
Struts: top, left, right
Springs: both

UIButton – nothing set, i.e., flexible everywhere

Although my light bulb is dim, there appears to be hope.

How do I get the height and width of the Android Navigation Bar programmatically?

Try below code:

Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;

How do I get the height and width of the Android Navigation Bar programmatically in flutter?

Use this to get the bottom nav bar height

MediaQuery.of(context).viewInsets.bottom

How to REALLY get the navigation bar height in Android

This is the code I use to get the navigation bar size. Its height will be in Point.y

Credit to this answer

public static Point getNavigationBarSize(Context context) {
Point appUsableSize = getAppUsableScreenSize(context);
Point realScreenSize = getRealScreenSize(context);

// navigation bar on the right
if (appUsableSize.x < realScreenSize.x) {
return new Point(realScreenSize.x - appUsableSize.x, appUsableSize.y);
}

// navigation bar at the bottom
if (appUsableSize.y < realScreenSize.y) {
return new Point(appUsableSize.x, realScreenSize.y - appUsableSize.y);
}

// navigation bar is not present
return new Point();
}

public static Point getAppUsableScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size;
}

public static Point getRealScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();

if (Build.VERSION.SDK_INT >= 17) {
display.getRealSize(size);
} else if (Build.VERSION.SDK_INT >= 14) {
try {
size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
} catch (IllegalAccessException e) {} catch (InvocationTargetException e) {} catch (NoSuchMethodException e) {}
}

return size;
}

Edit: To answer your question I had to use this function since I wanted to add ResideMenu to my app, but ended getting a weird empty margin at the bottom of my app, because of the navigation bar.

So I edited this function added by ResideMenu like this:

@Override
protected boolean fitSystemWindows(Rect insets) {
// Applies the content insets to the view's padding, consuming that content (modifying the insets to be 0),
// and returning true. This behavior is off by default and can be enabled through setFitsSystemWindows(boolean)
// in API14+ devices.

int bottomPadding = insets.bottom;

Point p = getNavigationBarSize(getContext());

if (Build.VERSION.SDK_INT >= 21 && p.x != 0) {
Resources resources = getContext().getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
bottomPadding += resources.getDimensionPixelSize(resourceId);
}
}

this.setPadding(viewActivity.getPaddingLeft() + insets.left, viewActivity.getPaddingTop() + insets.top,
viewActivity.getPaddingRight() + insets.right, viewActivity.getPaddingBottom() + bottomPadding);
insets.left = insets.top = insets.right = insets.bottom = 0;
return true;
}

Hope that will help you.

Programmatically change height of navigation bar in swift

update for iOS 11 and beyond

apple doesn't want you messing with the navigation bar height, so don't touch it
see here: https://openradar.appspot.com/32912789
and here: https://forums.developer.apple.com/thread/88202#274620

class TallerNaviBar: UINavigationBar {
override func sizeThatFits(size: CGSize) -> CGSize {
var newSize:CGSize = CGSizeMake(self.superview!.frame.size.width, 87)
return newSize
}
}

I don't do swift, but the answer is easy, here's ObjC

- (CGSize)sizeThatFits:(CGSize)size {

return CGSizeMake([self superview].frame.size.width, 40);

}

Here's my interpretation in Swift:

import UIKit
class TallerNaviBar: UINavigationBar {
override func sizeThatFits(size: CGSize) -> CGSize {
var newSize:CGSize = CGSizeMake(superview.width, 87)
return newSize
}
}

The problem you will have isn't with this method, this is the easy part, the problem is forcing the navigation controller to always use this navigation bar

I subclass and resize everything in IOS, including navigation controllers and tabbarcontrollers, in order to enforce that all navigation controllers use this navigation bar, you must subclass a navigationController and only use this navigationcontroller throughout your app, here's how the subclass works, this is Obj C, so you'll have to translate it:

@interface NSHNavigationController () <UINavigationBarDelegate, UINavigationControllerDelegate>
{

}

@implementation NSHNavigationController

#pragma mark Initialization

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self NSHSetupNavigationController];
}
return self;
}

- (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass
{
self = [super initWithNavigationBarClass:navigationBarClass toolbarClass:toolbarClass];
if (self) {
[self NSHSetupNavigationController];
}
return self;
}

- (id)initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithRootViewController:rootViewController];
if (self) {
[self NSHSetupNavigationController];
}
return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self NSHSetupNavigationController];
}
return self;
}

- (void)dealloc
{
[self setInternalDelegate:nil];
[self setExternalDelegate:nil];
}

#pragma mark Setup

- (void)NSHSetupNavigationController
{
[self setValue:[[NSHNavigationBar alloc]init] forKeyPath:@"navigationBar"];
}

this is the line that will do it for you:

   [self setValue:[[NSHNavigationBar alloc]init] forKeyPath:@"navigationBar"];

Oh yeah, and make sure you are subclassing the nav bar, you said you were, but here's how you do it, it's simple:

#import "NSHNavigationBar.h"

@implementation NSHNavigationBar
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSDictionary *attributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],
NSFontAttributeName:fontMagicForRegularNSHFont};
[self setTitleTextAttributes:attributes];
[self setTranslucent:true];
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (CGSize)sizeThatFits:(CGSize)size {

return CGSizeMake([self superview].frame.size.width, heightResizer(40));

}

- (void)layoutSubviews {

[super layoutSubviews];

}

So, in summary, subclass both the UINavigationBar and the UINavigationController and you are set, this will allow you to manipulate the navigation bar whenever you'd like, you can also type cast your view controller's navigation bars, this is a little nuts and will confuse a lot of people, but here it goes:

-(CustomNavigationBar *)navBar {

return (id)[self.navigationController navigationBar];
}

put the stuff above in your view controller and then you call it like this:

[[self navBar] setBackGroundColor:[UIColor blueColor]];

This will successfully typecast your navigationcontroller to be your custom navigation bar, if you want to change the height of the nav bar from here, then you can do something like this:



Related Topics



Leave a reply



Submit