TomasJ
TomasJ

Reputation: 279

How to Hide UINavigationBar

Is it possible to put view (object) which will cover navigation bar? I simply can't put it there. It allways hides under nav. bar.

Upvotes: 1

Views: 5167

Answers (3)

richardjsimkins
richardjsimkins

Reputation: 413

If your UIViewController is under a UITabBarController then [self navigationController] is not set until after viewDidLoad is called as its layout is not determined until its corresponding tab is selected. By which point the state of [self navigationController] has probably been changed by iOS so that the navigation bar is visible again.

For changes that affect the layout of the view it is always better to do these within the UIViewController method viewDidLayoutSubviews. By which point in the UIViewController execution [self navigationController] will always be set if there is one and its state will not be changed further by iOS.

viewDidLoad should ideally only be used to instantiate content for a view and never to position them within the layout. Since changing the navigation bar's visibility affects the layout of the view, setting its visibility within viewDidLoad may result in undefined behaviour.

- (void) viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [[self navigationController]
        setNavigationBarHidden:YES
        animated:NO];
}

Upvotes: 0

Jorn van Dijk
Jorn van Dijk

Reputation: 1993

You can do the following in the viewDidLoad of one of your viewControllers:

- (void)viewDidLoad 
{
    [super viewDidLoad];
    UIView* navigationBarCover = [[UIView alloc] initWithFrame:self.navigationController.navigationBar.frame];
    navigationBarCover.backgroundColor = [UIColor blackColor];
    [self.navigationController.view addSubview:navigationBarCover];
}

You can also hide the navigationBar and put the view on the viewControllers view:

- (void)viewDidLoad 
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    UIView* navigationBarCover = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    navigationBarCover.backgroundColor = [UIColor redColor];
    [self.view addSubview:navigationBarCover];
}

Upvotes: 3

Tim Vermeulen
Tim Vermeulen

Reputation: 12552

I wouldn't suggest trying to cover your navigation bar, as it's there for a reason. However, to hide it completely, you could send your navigation controller the setNavigationBarHidden:animated: method.

Upvotes: 10

Related Questions