在IOS swift应用程序中隐藏选项卡栏
我想知道如何在iOS swift应用程序中隐藏标签栏。我不在乎任何花哨的动画或任何东西。我可以在ViewDidLoad()函数中添加一些内容。您只需在在IOS swift应用程序中隐藏选项卡栏,ios,swift,Ios,Swift,我想知道如何在iOS swift应用程序中隐藏标签栏。我不在乎任何花哨的动画或任何东西。我可以在ViewDidLoad()函数中添加一些内容。您只需在ViewDidLoad()方法中使用它即可 self.tabBarController?.tabBar.hidden = true 对于Swift 3.0、4.0、5.0: self.tabBarController?.tabBar.isHidden = true 也可以通过以下方式更改选项卡栏的z位置: self.tabBarControll
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
按下前设置controller.hidesbottombar whenpushed=true
接受的答案有效,但到其他视图的过渡有一个不连续的动画(选项卡栏动画)
还想补充一点,尽管Kalpesh的解决方案对我来说非常有效,但我发现每个视图控制器都有一个hidesBottomBarWhenPushed属性(请查看故事板)。如果您希望隐藏选项卡栏,应该在该属性上打勾。这将非常有效
更新:我不确定这是否是已知的,但以下是苹果文档页面上的内容:
我认为这意味着您必须在最顶端的视图控制器(导航堆栈上的第一个)上设置HidesBottomBar的基本值。一旦将其设置为true,您就可以为堆栈上的其他视图控制器更改为false或true。但是,如果在Pushed值为false时,最顶层视图控制器的HidesBottomBar将不会显示导航堆栈上其他控制器的选项卡栏。Swift 3
self.tabBarController?.tabBar.isHidden = true
无需设置tabBar的isHidden属性
简单地说,转到ViewController(在故事板中)->属性检查器->在“视图控制器”部分下选择“按下时隐藏底部栏”复选框。这很有魅力
如果您使用“isHidden”方式,则需要进行大量处理,即在返回时使其再次出现,并在隐藏tabBar后删除底部的空白。您也可以在扩展中设置它(使用Dharmesh Kheni answer)
这是Swift4.0、4.1、4.2、5.0及更高版本的编程方式:
tabBarController?.hidesBottomBarWhenPushed = true
或
要隐藏导航栏和选项卡栏,我使用下一个函数:
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的高度也会改变,因此我使用下一个函数退出fullscreen来调整高度:
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
}
}
我希望它对您有用。这是我的代码。只是为了隐藏它的标签栏。(如果没有很好地建立框架,则底部将显示黑色视图。)
达尔梅什,谢谢你的回复。这成功地隐藏了选项卡栏,但是表视图并没有填满屏幕。标签栏应该在的地方是黑色的。我想还有另一个步骤使它适合屏幕?为此添加约束。要添加的“约束”是什么?如果使用约束,则可以更改高度约束,也可以更改表格视图的框架@DB0但选项卡栏仍会保持接触,请尝试在不可见选项卡栏的任何选项卡附近轻敲@达尔梅什凯尼那是我们应该做的。如果您只想在显示ViewController时隐藏它,那么比其他答案要好得多为什么这在Swift 4中不起作用??我已经勾选了该选项,但当我转到该特定视图控制器时,它仍然会出现。这还隐藏了在此之后添加到堆栈中的任何视图控制器的选项卡栏。有没有办法为后续的视图控制器恢复它?@justinvalley请让我知道更新是否在回答中有效谢谢,接受的回答对我来说有问题,当我返回到我来自的视图时,该选项卡栏也被取消了!这应该是实际的答案。这使我可以轻松地设置UI元素的约束。推送时隐藏底部栏
的动画效果不好,我想。当你从这个页面返回时,Tabbar会冻结一点,然后返回。你认为原因是什么?@NicoS。对。在问题/答案中,您在哪里读到有关工具栏的内容?来自文档:一个布尔值,指示当视图控制器被推到导航控制器上时,屏幕底部的工具栏是否隐藏。问题是关于选项卡栏而不是工具栏。第一个工具栏不起作用,第二个选项应在执行segue时或在按下视图控制器之前添加。感谢:destinationVC.hidesBottomBarWhenPushed=true。对于所有其他选项,底部栏已经消失,但有一个大黑洞。在按下VC之前设置此选项vc.hidesbottombar whenpushed=true
应该可以完成这项工作。不要手动显示和隐藏选项卡栏。使用setViewControllers
hidesBottomBarWhenPushed = true
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
}
}
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
}
}
var oldTabbarFr: CGRect = .zero
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
oldTabbarFr = self.tabBarController?.tabBar.frame ?? .zero
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = true
self.tabBarController?.tabBar.frame = .zero
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.tabBarController?.tabBar.isHidden = false
self.tabBarController?.tabBar.frame = oldTabbarFr
}