我正在使用一个非常简单的设置,并以编程方式实例化了UITabBarController
。
我的应用程序的配置来自服务器,并且基于该配置,viewControllers
属性是动态设置的。这意味着viewController(以及选项卡)的数量可以随时更改。
除了一件事之外,这一切都很好。
当tabBar
在viewControllers数组中只有1个viewController时,我想隐藏UITabBarController
。
我知道您可以在viewController上为新推的viewControllers设置一个标志以隐藏底部的栏,但这不是我想要的。
我想以编程方式控制tabBar的可见性,并且显然还设置了所有正确的安全空白,以便子viewController获得所有可能的空间。
答案 0 :(得分:1)
我遇到过这种情况,我使用了下面的代码,这些代码成功地为我工作:
extension UITabBarController {
func setTabBarVisible(visible:Bool, duration: TimeInterval = 0.20, animated:Bool) {
if (tabBarIsVisible() == visible) { return }
let frame = self.tabBar.frame
let height = frame.size.height
let offsetY = (visible ? -height : height)
let duration = animated ? duration : 0
var safeAreaInset:CGFloat = 0
if #available(iOS 11, *) {
safeAreaInset = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
safeAreaInset += visible ? (UIApplication.shared.keyWindow?.safeAreaInsets.top ?? 0) : 0
}
if !visible, let window = self.view.window {
if let view = window.viewWithTag(999) {
view.removeFromSuperview()
}
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
window.insertSubview(view, at: 0)
view.tag = 999
view.leadingAnchor.constraint(equalTo: window.leadingAnchor).isActive = true
view.trailingAnchor.constraint(equalTo: window.trailingAnchor).isActive = true
view.bottomAnchor.constraint(equalTo: window.bottomAnchor).isActive = true
view.heightAnchor.constraint(equalToConstant: safeAreaInset).isActive = true
view.backgroundColor = .white
window.sendSubview(toBack: view)
}
let viewFrame = CGRect(x:self.view.frame.origin.x,y:self.view.frame.origin.y,width: self.view.frame.width, height: (self.view.frame.height + offsetY - safeAreaInset))
// animation
UIView.animate(withDuration: duration, animations: {
self.tabBar.frame.offsetBy(dx:0, dy:offsetY)
self.view.frame = viewFrame
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}) { (finished) in
if finished {
if let view = self.view.window?.viewWithTag(999) {
self.view.window?.sendSubview(toBack: view)
}
}
}
}
func tabBarIsVisible() ->Bool {
return self.tabBar.frame.origin.y < UIScreen.main.bounds.height
}
}
您可以像这样使用它:
具有一个本地变量,该变量将存储可见性状态并在设置时更新标签栏:
var tabbarHidden = false {
didSet {
self.tabbarController.setTabBarVisible(visible: !tabbarHidden, animated: false)
self.tabbarController.tabBar.isHidden = tabbarHidden
}
}
具有设置标签栏可见性的功能:
func setTabbarVisibility() {
if !((self.tabbarController.selectedViewController as? UINavigationController)?.topViewController?.hidesBottomBarWhenPushed ?? false) {
let count = Globals.sharedInstance.currentEvent?.bottom.count ?? 0
self.tabbarHidden = count == 0
}
}
在viewWillAppear
和viewDidLayoutSubviews
中调用此函数以更新设置,因为设置来自服务器。
答案 1 :(得分:0)
在您的CustomTabBarViewController中编写以下代码。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
hideTabBar() //Calling function to hide tabbar
}
//Call below function when you want hide tabbar
func hideTabBar() {
guard let firstViewController = self.viewControllers?.first as? TableListViewController else {
return
}
DispatchQueue.main.async {
self.tabBar.isHidden = true
firstViewController.view.frame.size.height = UIScreen.main.bounds.size.height
}
}
//Call below function when you want unhide tabbar
func unHideTabBar() {
guard let firstViewController = self.viewControllers?.first as? TableListViewController else {
return
}
DispatchQueue.main.async {
self.tabBar.isHidden = false
firstViewController.view.frame.size.height = UIScreen.main.bounds.size.height - self.tabBar.frame.size.height
}
}
希望这对您有帮助!