Skip to content

wicharek/UINavigationBar-FixedHeightWhenStatusBarHidden

Repository files navigation

UINavigationBar-FixedHeightWhenStatusBarHidden

Version License Platform

Normally on iOS 7+ navigation bar height equals to 64 px, when status bar is shown. After it is hidden, the height is changed to 44 px by default.

This category adds property fixedHeightWhenStatusBarHidden to UINavigationBar class. If set to YES navigation bar will keep its size even if status bar was hidden.

This is especially useful in case of "drawer"-styled side panel, when you want status bar to be hidden when this panel is shown.

Installation

Manual:
Copy source files from UINavigationBar-FixedHeightWhenStatusBarHidden folder to your Xcode project.

CocoaPods:
UINavigationBar-FixedHeightWhenStatusBarHidden is available through CocoaPods. To install it, add the following line to your Podfile:

pod 'UINavigationBar-FixedHeightWhenStatusBarHidden'

How to Use

Add #import "UINavigationBar+FixedHeightWhenStatusBarHidden.h".

Enable fixed height with:

self.navigationController.navigationBar.fixedHeightWhenStatusBarHidden = YES;

On iOS 11+ you should also set correct additional safe area insets on each view controller presented inside UINavigationController with fixed height enabled. This can be done by overriding viewWillLayoutSubviews of UIViewController. Then you can install the required insets automatically:

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self.navigationController.navigationBar setAdditionalSafeAreaInsetsForViewController:self];	
}

Or in case you need to customize insets, use [UINavigationBar additionalSafeAreaInsets] selector to get insets required for fixed height to work and modify them according to your needs:

- (void)viewWillLayoutSubviews {
   [super viewWillLayoutSubviews];

    if (@available(iOS 11.0, *)) {
        UIEdgeInsets navigationBarSafeAreaInsets = [self.navigationController.navigationBar additionalSafeAreaInsets];
        self.additionalSafeAreaInsets = UIEdgeInsetsMake(navigationBarSafeAreaInsets.top + 20, 0, 0, 0);
    }
}

License

UINavigationBar-FixedHeightWhenStatusBarHidden is available under the MIT license. See the LICENSE file for more info.