在 iOS 8 中推送/弹出视图时显示/隐藏导航栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25866743/
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
Show/hide navigationBar when view is pushed/popped in iOS 8
提问by nebuto
Hi fellow developers,
各位开发者好,
I'm suprised that I didn't find any information on that particular use case for iOS 8. Let me explain, I have a navigation controller stack, the first view has it's navigationBar hidden, when the user clicks on a cell a new view is pushed and I need the navigation bar shown on this view to go back (obviously). On iOS 7 it was just a matter of adding this line in viewWillAppear :
我很惊讶我没有找到关于 iOS 8 的那个特定用例的任何信息。让我解释一下,我有一个导航控制器堆栈,第一个视图隐藏了它的导航栏,当用户点击一个新视图时被推动,我需要这个视图上显示的导航栏返回(显然)。在 iOS 7 上,只需在 viewWillAppear 中添加这一行:
[self.navigationController setNavigationBarHidden:NO animated:YES];
And it works like a charm. But as of iOS 8 I'm struggling to have the same behavior. For now I managed to do the same by using :
它就像一个魅力。但是从 iOS 8 开始,我很难有相同的行为。现在我设法通过使用来做同样的事情:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void)viewDidLoad {
[super viewDidLoad];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
self.automaticallyAdjustsScrollViewInsets = NO;
self.edgesForExtendedLayout = UIRectEdgeNone;
self.collectionView.contentInset = UIEdgeInsetsMake(self.navigationController.navigationBar.frame.size.height, 0, 0, 0);
} }
And changing the contentInset of my collectionsView / tableviews whatever I need to display. It'll work, but it's a real pain in the *(sometimes the content inset isn't necessary if the user comes from a new viewcontroller).
并更改我的 collectionsView/tableviews 的 contentInset,无论我需要显示什么。它会工作,但它是*的真正痛苦(如果用户来自新的视图控制器,有时不需要内容插入)。
For context the project was first developed to handle iOS 6 and 7, so no storyboard, all is done programmatically. I ported the app to a more modern codebase and made it universal.
对于上下文,该项目最初是为处理 iOS 6 和 7 而开发的,因此没有故事板,一切都以编程方式完成。我将该应用程序移植到更现代的代码库并使其通用。
Am I missing something ? Is there a better way to handle that in iOS 7/8 ?
我错过了什么吗?在 iOS 7/8 中有没有更好的方法来处理这个问题?
回答by Nick Wargnier
You want to hide it in viewWillAppear
and show it again in viewWillDisappear
您想将其隐藏viewWillAppear
并再次显示在viewWillDisappear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.isHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.navigationBar.isHidden = false
}
回答by Patrick Haaser
Do this animated to get a smooth transition:
执行此动画以获得平滑过渡:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
回答by iHart
I thought line is :
我认为线是:
self.navigationController.navigationBar.hidden = YES;
I check for it and it's work fine for me . . . Try it . . .
我检查了它,它对我来说很好用。. . 尝试一下 。. .
回答by de-bits
On iOS 8
, it seems navigationBarHidden
is not the same as navigationBar.hidden
.
(I am building with deployment target iOS 6
-- not sure if this makes a difference).
对iOS 8
,好像navigationBarHidden
不太一样navigationBar.hidden
。(我正在使用部署目标进行构建iOS 6
- 不确定这是否有所作为)。
In my code, looking at the debugger with following consecutivelines:
在我的代码中,使用以下连续行查看调试器:
bool b1 = self.navigationController.navigationBarHidden; // FALSE
bool b2 = self.navigationController.navigationBar.hidden; // TRUE
b1
shows as false and b2
as true.
b1
显示为假和b2
真。
Setting self.navigationController.navigationBar.hidden = false
solves the problem
设置self.navigationController.navigationBar.hidden = false
解决问题
回答by Roozbeh Zabihollahi
I had the same problem and I really tried everything, but the navigation bar did not show up. Finally I realized I had this code in my first view controller :-)
我遇到了同样的问题,我真的尝试了一切,但导航栏没有出现。最后我意识到我的第一个视图控制器中有这段代码:-)
[self.navigationController setNavigationBarHidden:YES];
I thought, somebody else that checks this question, did my mistake, so I thought it may worth mentioning.
我想,检查这个问题的其他人犯了我的错误,所以我认为值得一提。
回答by gontovnik
There are multiple techniques. The less friction one would be:
有多种技术。摩擦较小的是:
open class ViewController: UIViewController {
open var hidesNavigationBarWhenPushed = false
override open func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(hidesNavigationBarWhenPushed, animated: animated)
}
}
But that would give you this result.
但这会给你这个结果。
Alternatively, I would suggest using my newly built HidesNavigationBarWhenPushed library. Here is the explanationof how it works and why I built it.
或者,我建议使用我新建的HidesNavigationBarWhenPushed 库。这是它的工作原理以及我构建它的原因的解释。