我试图弄清楚如何在我的iOS swift应用程序中隐藏选项卡栏。我不在乎任何精美的动画或任何东西。我可以在ViewDidLoad()函数中放一些东西。
Answers:
您可以在您的ViewDidLoad()
方法中简单地使用它。
self.tabBarController?.tabBar.hidden = true
对于Swift 3.0、4.0、5.0:
self.tabBarController?.tabBar.isHidden = true
或者,您可以通过以下方式更改标签栏的z位置:
self.tabBarController?.tabBar.layer.zPosition = -1
如果您想再次显示它,则:
self.tabBarController?.tabBar.layer.zPosition = 0
可接受的答案有效,但是过渡到其他视图时会出现断断续续的动画(选项卡栏动画)
尽管Kalpesh的解决方案非常适合我,但我也想补充一点,我发现每个视图控制器都有一个hidesBottomBarWhenPushed属性(请检查情节提要)。如果要隐藏选项卡,则应在其上打勾。而且效果很好。
更新:我不确定这是否是已知的东西,但是这是Apple文档页面所说的内容:
作为导航控制器的子级添加的视图控制器可以在屏幕底部显示一个可选工具栏。最顶部视图控制器上此属性的值确定工具栏是否可见。如果此属性的值为true,则工具栏将隐藏。如果此属性的值为false,则该条可见。
我认为这意味着您必须在最顶部的视图控制器(导航堆栈中的第一个)上设置hidesBottomBarWhenPushed的基本值。将其设置为true时,可以将其上其他视图控制器的false或true更改为true。堆栈。但是,如果最高视图控制器的hidesBottomBarWhenPushed值为false,则不会在导航堆栈中显示其他控制器的标签栏。
无需设置tabBar的isHidden属性。
只需简单地转到ViewController(在StoryBoard中)->属性检查器->在“视图控制器”部分下,选择“在推送时隐藏底部栏”复选框。这就像一个魅力。
如果您采用“ isHidden”方式,则需要进行大量处理,即使其返回时再次出现,并在隐藏tabBar之后删除底部的空白区域。
Hide Bottom Bar on Push
动画imo
您也可以将其设置为扩展名(使用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
}
}
这是Swift 4.0、4.1、4.2、5.0及更高版本的编程方式>:
tabBarController?.hidesBottomBarWhenPushed = true
要么
hidesBottomBarWhenPushed = true
要隐藏navigationBar和tabBar,我使用下一个函数:
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
}
}
当屏幕方向改变了时,tabBar的高度也改变了,所以我使用下一个函数退出全屏来调整高度:
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
}
}
希望对您有用。
vc.hidesBottomBarWhenPushed = true
应该做的工作。不要手动显示和隐藏标签栏。