76

I've created a two splash screen iPhone app. Afterwards user is taken to first view. I've added a UINavigationController. It works perfectly fine.

How do I remove the navigation bar for the opening view alone?

MainWindow

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    


self.splashScreen = [[SplashScreen alloc] 
                initWithNibName:@"SplashScreen" 
                bundle:nil];
if (self.pageController == nil) {
openingpage *page=[[openingpage alloc]initWithNibName:@"openingpage" bundle:[NSBundle mainBundle]];
    self.pageController = page;
    [page release];
}
[self.navigationController pushViewController:self.pageController animated:YES];

[window addSubview:splashScreen.view];

 [splashScreen displayScreen];
[self.window makeKeyAndVisible];

return YES;
 }
kingston
  • 1,553
  • 4
  • 22
  • 42

8 Answers8

163

Try this method inside a view controller:

// swift
self.navigationController?.setNavigationBarHidden(true, animated: true)

// objective-c
[self.navigationController setNavigationBarHidden:YES animated:YES]; 

More clarifications:

UINavigationController has a property navigationBarHidden, that allows you to hide/show the navigation bar for the whole nav controller.

Let's look at the next hierarchy:

--UINavigationController
------UIViewController1
------UIViewController2
------UIViewController3

Each of three UIViewController has the same nav bar since they are in the UINavigationController. For example, you want to hide the bar for the UIViewController2 (actually it doesn't matter in which one), then write in your UIViewController2:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];   //it hides the bar
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES]; // it shows the bar back
}
beryllium
  • 29,669
  • 15
  • 106
  • 125
  • 1
    Whilst this provides an answer giving an adequate result, the opening view/splash screen is not part of the navigation controller hierarchy, so shouldn't really be in the navigation controller. – twilson Feb 11 '12 at 09:50
  • 2
    what i want is to hide the navigation bar for only the first "opening view".using the above hides the entire navigation bar of the files related to the first view controller – kingston Feb 11 '12 at 09:53
  • @kingston, Then show it again `[self.navigationController setNavigationBarHidden:NO];` in viewWillDisappear for example :) – beryllium Feb 11 '12 at 09:57
  • @berylliyum:my opening page is a table view.ive set [self.navigationController setNavigationBarHidden:YES];this hides the nav bar of that particular tableview and also the navigated view from my table view say "mapview".so i used [self.navigationController setNavigationBarHidden:NO];in my "MapView" viewDidLoad i was able to get the navbar.but if i again navigate back to my opening view wich contains table view.i m getting the navbar once again any idea..i d be greatful if u could helpe me out of this – kingston Feb 11 '12 at 10:07
  • 4
    There is also `- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated` method which shows/hides navigation bar with animation of view. This answer led me to it. Thanks. – Martin Berger Jul 17 '13 at 14:15
  • This is good, except when you present a view modally you get an ugly animation of the navigation bar appearing – Sam Nov 13 '15 at 13:38
22

Swift 4:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    navigationController?.setNavigationBarHidden(true, animated: true)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(true)
    navigationController?.setNavigationBarHidden(false, animated: false)
}
Doe Jane
  • 221
  • 2
  • 2
  • Can you elaborate on your answer? Posting only code is often not very helpful. – Noel Widmer Aug 09 '17 at 10:23
  • This is good since it will show the navigation bar for the rest of the view controllers after you leave the current one :). Thank you! –  Jan 04 '18 at 18:25
6

This is worked for me:

Swift 4

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    navigationController?.setNavigationBarHidden(true, animated: false)
}

//reappears navigation bar on next page
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    navigationController?.setNavigationBarHidden(false, animated: true)
}
Dimitrios
  • 61
  • 1
  • 3
1

Use below one line code to hide Navigation bar in Swift3 and Swift4

navigationController?.setNavigationBarHidden(true, animated: true)

To show Navigation bar

navigationController?.setNavigationBarHidden(false, animated: true)
Sudhir kumar
  • 659
  • 5
  • 21
1

It is better to remember if it was hidden previously:

private var navigationBarWasHidden = false

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Save if it was hidden initially
    self.navigationBarWasHidden = self.navigationController?.isNavigationBarHidden ?? false
    // Hide the Navigation Bar
    self.navigationController?.setNavigationBarHidden(true, animated: animated)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // Show the Navigation Bar
    self.navigationController?.setNavigationBarHidden(self.navigationBarWasHidden, animated: animated)
}
0

In c# or Xamarin.IOS, this.NavigationController.NavigationBar.Hidden = true;

0

you can try hide navigationbar directly like this to related UIViewController

override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.navigationBar.isHidden = true
    super.viewWillAppear(animated)
}

override func viewWillDisappear(_ animated: Bool) {
    self.navigationController?.navigationBar.isHidden = false
    super.viewWillDisappear(animated)
}
-2

Present the opening view modally, or;

  1. don't add it to your navigation controller
  2. present it before the navigation controller.
  3. Once everything has loaded, dismiss the opening view and show the navigation controller (both without animation).

Taking an example from this thread: How can I display a splash screen for longer on an iPhone?

-(void)applicationDidFinishLaunching:(UIApplication *)application {
    [window addSubview:splashView];
    [NSThread detachNewThreadSelector:@selector(getInitialData:) 
                                 toTarget:self withObject:nil];
}

-(void)getInitialData:(id)obj {
    [NSThread sleepForTimeInterval:3.0]; // simulate waiting for server response
    [splashView removeFromSuperview];
    [window addSubview:tabBarController.view];
}
Community
  • 1
  • 1
twilson
  • 2,062
  • 14
  • 19
  • OP didn't mean how to display splash screen longer, and question isn't about splash at all. – beryllium Feb 11 '12 at 10:35
  • @berryllium Maybe he didn't mean that, but he said "a two splash screen iPhone App", so that's what he got answers about "splash screens". No user interaction was expressed in the question. – twilson Feb 11 '12 at 10:40