在 IOS swift 应用程序中隐藏标签栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28777943/
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
Hide tab bar in IOS swift app
提问by Robert
I'm trying to figure out how to hide the tab bar in my iOS swift app. I don't care about any fancy animations or anything. Just something I can put in the ViewDidLoad() function.
我想弄清楚如何在我的 iOS swift 应用程序中隐藏标签栏。我不在乎任何花哨的动画或任何东西。只是我可以放在 ViewDidLoad() 函数中的东西。
回答by Dharmesh Kheni
You can simply use this in your ViewDidLoad()
method.
您可以简单地在您的ViewDidLoad()
方法中使用它。
self.tabBarController?.tabBar.hidden = true
For Swift 3.0, 4.0, 5.0:
对于 Swift 3.0、4.0、5.0:
self.tabBarController?.tabBar.isHidden = true
Or you can change z position of tab bar this way:
或者您可以通过这种方式更改标签栏的 z 位置:
self.tabBarController?.tabBar.layer.zPosition = -1
and if you want to show it again then:
如果你想再次显示它,那么:
self.tabBarController?.tabBar.layer.zPosition = 0
回答by Akshansh Thakur
The accepted answer works, but the transition to other view has a choppy animation (The tab Bar animation)
接受的答案有效,但到其他视图的过渡有断断续续的动画(选项卡栏动画)
Also wanted to add although Kalpesh's solution worked perfectly for me, I found out that every view controller has an attribute for hidesBottomBarWhenPushed (check out storyboard.) If you wish to hide tab bar, you should put a tick on that. And it would work great.
还想补充一点,尽管 Kalpesh 的解决方案对我来说非常有效,但我发现每个视图控制器都有一个 hidesBottomBarWhenPushed 属性(查看故事板。)如果你想隐藏标签栏,你应该在上面打勾。它会很好用。
Update: Im not sure if this is a known thing, but here's what apple documentation page says:
更新:我不确定这是否是已知的事情,但这是苹果文档页面所说的:
I think this means that you have to set the basic value of hidesBottomBarWhenPushed at the topmost view controller (the first one on the navigation stack.) Once you have set that to true, you can change to false or true for the other viewcontrollers on the stack. But, if your topmost view controller's hidesBottomBarWhenPushed value is false, it will not show a tab bar for other controllers on the navigation stack.
我认为这意味着您必须在最顶层的视图控制器(导航堆栈中的第一个)设置 hidesBottomBarWhenPushed 的基本值。一旦将其设置为 true,您可以将其他视图控制器更改为 false 或 true。堆。但是,如果您最顶层的视图控制器的 hidesBottomBarWhenPushed 值为 false,则导航堆栈上的其他控制器将不会显示选项卡栏。
回答by kalpesh jetani
Before push set controller.hidesBottomBarWhenPushed = true
推送前设置 controller.hidesBottomBarWhenPushed = true
let objCreateEventVC = CreateEventVC()
objCreateEventVC.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(objCreateEventVC, animated: false)
回答by Tejas
No need to set tabBar's isHidden property.
无需设置 tabBar 的 isHidden 属性。
Simply, Go to ViewController (in StoryBoard) -> Attribute inspector -> Under 'View Controller' section select 'Hide Bottom Bar on Push' checkbox. This works like a charm.
简单地,转到 ViewController(在 StoryBoard 中)-> 属性检查器-> 在“视图控制器”部分下选择“推送时隐藏底部栏”复选框。这就像一个魅力。
If you go the 'isHidden' way you need to do a lot of handling, i.e. to make it appear again when you go back and also to remove the bottom empty space after hiding tabBar.
如果您采用“isHidden”方式,您需要进行大量处理,即在您返回时使其再次出现,并在隐藏 tabBar 后移除底部的空白区域。
回答by Ronny Kibet
Swift 3.
斯威夫特 3.
self.tabBarController?.tabBar.isHidden = true
回答by Yair hadad
You can also set it in extension (use Dharmesh Kheni answer)
您也可以在扩展中设置它(使用 Dharmesh Kheni 答案)
extension UITabBar {
func tabsVisiblty(_ isVisiblty: Bool = true){
if isVisiblty {
self.isHidden = false
self.layer.zPosition = 0
} else {
self.isHidden = true
self.layer.zPosition = -1
}
}
回答by J. Doe
This is the way programmatically for Swift 4.0, 4.1, 4.2, 5.0 and later>:
这是 Swift 4.0、4.1、4.2、5.0 及更高版本的编程方式>:
tabBarController?.hidesBottomBarWhenPushed = true
or
或者
hidesBottomBarWhenPushed = true
回答by Alberto
To hide the navigationBar and the tabBar I use the next function:
要隐藏导航栏和标签栏,我使用下一个函数:
var tabBarHeight : CGFloat!
func fullScreenAction(){
if navigationController?.isNavigationBarHidden ?? false {
//Show navigationBar
navigationController?.setNavigationBarHidden(false, animated: false)
//Show tabBar
tabBarController?.tabBar.isHidden = false
//Update the height of tabBar
if (!(tabBarController?.tabBar.frame.size.height.isEqual(to: 0))!) {
tabBarHeight = self.tabBarController?.tabBar.frame.size.height
}
tabBarController?.tabBar.frame.size.height = tabBarHeight
} else {
//Hide navigationBar
navigationController?.setNavigationBarHidden(true, animated: false)
//Hide tabBar
tabBarController?.tabBar.isHidden = true
//Update the height of tabBar
tabBarHeight = tabBarController?.tabBar.frame.size.height
tabBarController?.tabBar.frame.size.height = 0
}
}
When the screen orientation has changed the height of tabBar change too, so I use the next function to exit of fullscreen to resize the height:
当屏幕方向改变了 tabBar 的高度也改变了,所以我使用 next 函数退出全屏来调整高度:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if navigationController?.isNavigationBarHidden ?? false {
navigationController?.setNavigationBarHidden(false, animated: false)
tabBarController?.tabBar.isHidden = false
}
}
I hope it is useful for you.
我希望它对你有用。