2
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)
            }
        })
    }

}

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

4

3 回答 3

0

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

创建UIViewController命名BaseViewController并使其成为所有视图控制器的基类

现在定义协议

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 {
}

希望对您有所帮助

于 2018-08-25T04:59:47.133 回答
0

测试 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
    }

希望对您有所帮助

于 2018-08-25T06:43:01.967 回答
0

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
    }

}
于 2018-08-25T07:32:12.557 回答