在点击视图的一部分时,如何真正隐藏和显示标签栏? (没有按钮,但屏幕的任何位置)

How do you really hide and show a tab bar when tapping on part of your view? (no buttons but any location of the screen)

override func viewDidLoad() {        

    let tap = UITapGestureRecognizer(target: self, action: #selector(touchHandled))
    view.addGestureRecognizer(tap)


}


@objc func touchHandled() {
    tabBarController?.hideTabBarAnimated(hide: true)
}


extension UITabBarController {
    func hideTabBarAnimated(hide:Bool) {
        UIView.animate(withDuration: 2, animations: {
            if hide {
                self.tabBar.transform = CGAffineTransform(translationX: 0, y: 100)
            } else {
                self.tabBar.transform = CGAffineTransform(translationX: 0, y: -100)
            }
        })
    }

}

我只能隐藏标签栏,但当你再次点击时我无法让它显示出来。我试图寻找有关堆栈溢出的答案,但答案似乎只有在您使用按钮或情节提要时才有效。

具有适用于所有屏幕的协议的通用解决方案

创建名为 BaseViewControllerUIViewController 并使其成为所有视图控制器

的基础 class

现在定义协议

protocol ProtocolHideTabbar:class {
    func hideTabbar ()

}
protocol ProtocolShowTabbar:class {
    func showTabbar ()

}

extension ProtocolHideTabbar  where Self : UIViewController {
    func hideTabbar () {
        self.tabBarController?.tabBar.isHidden = true
    }
}
extension ProtocolShowTabbar  where Self : UIViewController {
    func showTabbar () {
        self.tabBarController?.tabBar.isHidden = false
    }
}

默认情况下,我们希望在每个视图控制器中显示标签栏,所以

extension UIViewController : ProtocolShowTabbar {}

在您的 BaseView 控制器中

在视图中将出现方法添加以下代码以显示基于协议的隐藏

    if self is ProtocolHideTabbar {
        ( self as! ProtocolHideTabbar).hideTabbar()
    } else  if  self is ProtocolShowTabbar{
        ( self as ProtocolShowTabbar).showTabbar()

    }

使用方法

简直

class YourViewControllerWithTabBarHidden:BaseViewController,ProtocolHideTabbar {
}

希望对您有所帮助

已测试 100% 正常工作

请在 UITabBarController 子类

中尝试以下代码
 var isTabBarHidden:Bool = false


 func setTabBarHidden(_ tabBarHidden: Bool, animated: Bool,completion:((Void) -> Void)? = nil) {
        if tabBarHidden == isTabBarHidden   {

            self.view.setNeedsDisplay()
            self.view.layoutIfNeeded()

            //check tab bar is visible and view and window height is same then it should be 49 + window Heigth

            if (tabBarHidden == true && UIScreen.main.bounds.height == self.view.frame.height) {
                let offset = self.tabBar.frame.size.height
                self.view.frame = CGRect(x:0, y:0, width:self.view.frame.width, height:self.view.frame.height + offset)

            }

            if let block = completion {

                block()
            }
            return
        }
        let offset: CGFloat? = tabBarHidden ? self.tabBar.frame.size.height : -self.tabBar.frame.size.height
        UIView.animate(withDuration: animated ? 0.250 : 0.0, delay: 0.1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [.curveEaseIn, .layoutSubviews], animations: {() -> Void in
            self.tabBar.center = CGPoint(x: CGFloat(self.tabBar.center.x), y: CGFloat(self.tabBar.center.y + offset!))

            //Check if View is already at bottom so we don't want to move view more up (it will show black screen on bottom ) Scnario : When  present mail app
            if (Int(offset!) <= 0 && UIScreen.main.bounds.height ==   self.view.frame.height) == false {
                self.view.frame = CGRect(x:0, y:0, width:self.view.frame.width, height:self.view.frame.height + offset!)
            }
            self.view.setNeedsDisplay()
            self.view.layoutIfNeeded()

        }, completion: { _ in
            if let block = completion {
                block()
            }
        })
        isTabBarHidden = tabBarHidden
    }

希望对您有所帮助

在 class 中有一个变量 isTabBarHidden,用于存储 tabBar 是否已通过动画隐藏。 (您可以使用 tabBar.isHidden,但是当动画隐藏和显示时,这会使逻辑复杂一点)

class ViewController {

    var isTabBarHidden = false // set the default value as required

    override func viewDidLoad() {        
        super.viewDidLoad()

        let tap = UITapGestureRecognizer(target: self, action: #selector(touchHandled))
        view.addGestureRecognizer(tap)
    }

    @objc func touchHandled() {
        guard let tabBarControllerFound = tabBarController else {
            return
        }
        tabBarController?.hideTabBarAnimated(hide: !isTabBarHidden)
        isTabBarHidden = !isTabBarHidden
    }

}