Mateusz
Mateusz

Reputation: 452

Custom bar between NavigationBar and controller's view

I need to add an offline indicator bar to the app. It needs to be between navigation bar and view controller's view. Offline bar needs to be visible all the time, no matter how many controllers are pushed. Bar cannot overlap anything, so adding subview to navigation bar is not an option.

First approach:

I have added container view and embedded a navigation controller in. The offline bar stays, but navigation bar is under it, and it can't.

Storyboard Current

Second approach:

Added main navigation controller for navigation bar presentation, wrapped Root into navigation controller. Presentation is proper until I tap "Push child" button. There is no connection between embedded navigation controller and topmost navigation bar, so no title change, no back buttons.

enter image description here enter image description here

Upvotes: 0

Views: 741

Answers (2)

user3820674
user3820674

Reputation: 262


I think you can do it easiest way. First of all you must subclass UINavigationBar. Then override sizeThatFits: and returns a larger size.

#import "MyNavBar.h"

const CGFloat navigationBarHeightIncrease = 38.f;

@implementation MyNavBar

- (CGSize)sizeThatFits:(CGSize)size {

    CGSize amendedSize = [super sizeThatFits:size];
    amendedSize.height += navigationBarHeightIncrease;

    return amendedSize;
}

@end


Then subcalss UINavigationController class and in viewDidLoad: add your custom view:

#import "MyNavController.h"

@interface MyNavController ()

@end

@implementation MyNavController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *redOfflineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 25)];
    redOfflineView.backgroundColor = [UIColor redColor];
    [self.navigationBar addSubview:redOfflineView];
}

@end


I found interesting solutions about nav bar here
Hope it helps.
Cheers.

Upvotes: 1

Praful Kadam
Praful Kadam

Reputation: 392

You can't take one bar above other since both the bars belong to different view controllers. but you can embed your container in navigation controller and show navigation bar of container and then set navigation bar of child view controller to hidden.

---------------- Updated description ----------------------

1) Embed your container view controller in Navigation controller( select container view controller in storyboard and then Editor > Embed in > Navigation controller).

2) Add your red coloured offline view on container view controller.

3) In child view controller's viewWillAppear

[self.navigationController setNavigationBarHidden:YES animated:NO];

so this way you can show top bar and red bar of your container view controller only and just top bar less view from child controller

Upvotes: 0

Related Questions