在 IOS 快捷应用中隐藏标签栏

我正在想办法把标签栏隐藏在我的 iOS 快捷应用里。我才不在乎什么花哨的动画片什么的。只是一些我可以放在 ViewDidLoad ()函数中的东西。

134353 次浏览

您可以简单地在 ViewDidLoad()方法中使用它。

self.tabBarController?.tabBar.hidden = true

对于 Swift 3.0,4.0,5.0:

self.tabBarController?.tabBar.isHidden = true

Or you can change z position of tab bar this way:

self.tabBarController?.tabBar.layer.zPosition = -1

如果你想再展示一次:

self.tabBarController?.tabBar.layer.zPosition = 0

在推送设置控制器之前

let objCreateEventVC = CreateEventVC()
objCreateEventVC.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(objCreateEventVC, animated: false)

Swift 3.

self.tabBarController?.tabBar.isHidden = true

不需要设置 tabBar 的 isHidden 属性。

简单地说,转到 ViewController (在 StoryBoard 中)-> Attribute spector-> 在“ View Controller”部分下选择“ Hide Bottom Bar on Push”复选框。这招真管用。

如果你使用“ isHidden”方式,你需要做很多处理,比如当你返回的时候让它再次出现,并且在隐藏标签栏之后移除底部的空白。

您也可以扩展设置它(使用 Darmesh 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
}
}

This is the way programmatically for Swift 4.0,4.1,4.2,5.0及更高版本 >:

tabBarController?.hidesBottomBarWhenPushed = true

或者

hidesBottomBarWhenPushed = true

为了隐藏导航栏和 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


}


}

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:

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.

Here is my code. it's just to hide its tabbar. (If no frames are well established there will be a black view at the bottom. )

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
}

使用动画隐藏和显示标签栏

对于那些寻找隐藏/显示标签栏与动画。

自从 iOS13以来,UITabBar 在动画方面的行为发生了变化。您不能再使用 CGAffineTransform,而是应该动画它的帧位置。

请看我的完整指南: 用动画隐藏和显示标签栏

隐藏标签栏:

UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.7, options: .curveEaseOut) {
if let tabBarFrame = self.tabBarController?.tabBar.frame {
self.tabBarController?.tabBar.frame.origin.y = navigationController.view.frame.maxY + tabBarFrame.height
}
navigationController.view.layoutIfNeeded()
} completion: { _ in
self.tabBarController?.tabBar.isHidden = true
}

显示标签栏:

UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.7, options: .curveEaseOut) {
if let tabBarFrame = self.tabBarController?.tabBar.frame {
self.tabBarController?.tabBar.frame.origin.y = navigationController.view.frame.maxY - tabBarFrame.height
}
navigationController.view.layoutIfNeeded()
}

确保在 UIView.animate中调用 layoutIfNeeded()以动画触发更改。