kingston
kingston

Reputation: 1553

How to hide a navigation bar from one particular view controller

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;
 }

Upvotes: 76

Views: 69632

Answers (8)

Imam Aly
Imam Aly

Reputation: 1

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)
}

Upvotes: 0

beryllium
beryllium

Reputation: 29767

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
}

Upvotes: 163

Oleksii Malyarenko
Oleksii Malyarenko

Reputation: 236

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)
}

Upvotes: 1

Anisetti Nagendra
Anisetti Nagendra

Reputation: 341

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

Upvotes: 0

Dimitrios
Dimitrios

Reputation: 61

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)
}

Upvotes: 6

Sudhir kumar
Sudhir kumar

Reputation: 731

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)

Upvotes: 1

Doe Jane
Doe Jane

Reputation: 221

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)
}

Upvotes: 22

twilson
twilson

Reputation: 2062

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];
}

Upvotes: -2

Related Questions