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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9239067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:46:06  来源:igfitidea点击:

How to hide a navigation bar from one particular view controller

ioscocoa-touch

提问by kingston

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.

我创建了一个两个启动画面的 iPhone 应用程序。之后用户被带到第一个视图。我添加了一个 UINavigationController。它工作得很好。

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

回答by beryllium

Try this method inside a view controller:

在视图控制器中试试这个方法:

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

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


More clarifications:

更多说明:

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

UINavigationController有一个属性 navigationBarHidden,它允许你隐藏/显示整个导航控制器的导航栏。

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:

三个 UIViewController 中的每一个都具有相同的导航栏,因为它们位于 UINavigationController 中。比如你想把UIViewController2的bar隐藏起来(其实在哪一个都没有关系),那么在你的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
}

回答by Doe Jane

Swift 4:

斯威夫特 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)
}

回答by Dimitrios

This is worked for me:

这对我有用:

Swift 4

斯威夫特 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)
}

回答by Alexey Malyarenko

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

回答by Sudhir kumar

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

使用下面一行代码隐藏 Swift3 和 Swift4 中的导航栏

navigationController?.setNavigationBarHidden(true, animated: true)

To show Navigation bar

显示导航栏

navigationController?.setNavigationBarHidden(false, animated: true)

回答by Anisetti Nagendra

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

在 c# 或 Xamarin.IOS 中,this.NavigationController.NavigationBar.Hidden = true;

回答by twilson

Present the opening view modally, or;

模态呈现开场视图,或;

  1. don't add it to your navigation controller
  2. present it beforethe navigation controller.
  3. Once everything has loaded, dismiss the opening view and show the navigation controller (both without animation).
  1. 不要将它添加到您的导航控制器
  2. 将其呈现导航控制器之前
  3. 加载完所有内容后,关闭打开视图并显示导航控制器(均没有动画)。


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

以此线程为例:如何在 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];
}