How to hide a navigation bar from one particular view controller
IosCocoa TouchIos Problem Overview
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;
}
Ios Solutions
Solution 1 - Ios
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
}
Solution 2 - Ios
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)
}
Solution 3 - Ios
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)
}
Solution 4 - Ios
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)
Solution 5 - Ios
In c# or Xamarin.IOS, this.NavigationController.NavigationBar.Hidden = true;
Solution 6 - Ios
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)
}
Solution 7 - Ios
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)
}
Solution 8 - Ios
Present the opening view modally, or;
- don't add it to your navigation controller
- present it before the navigation controller.
- Once everything has loaded, dismiss the opening view and show the navigation controller (both without animation).
Taking an example from this thread: https://stackoverflow.com/questions/553336/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];
}