如何从一个特定的视图控制器隐藏导航栏

我创建了一个两屏的 iPhone 应用程序。然后用户被带到第一个视图。我添加了一个 UINavigationController。完全没问题。

我如何删除导航栏的开放视图本身?

主窗口

- (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;
}
69101 次浏览

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

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
}

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

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)

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

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

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

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