0
votes

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

2
Just make the left most VC embedded in a navigation controller and add a bar in the view of that VC.Cheng-Yu Hsu
But then the bar will disappear after I push anything. I need the bar to be visible all the time, on any pushed view controller.Mateusz
You may add the bar to the navigation controller at runtime to achieve this.Cheng-Yu Hsu
Can you tell me how? Adding subview to navigation bar overlaps its content.Mateusz

2 Answers

1
votes


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.

0
votes

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