簡體   English   中英

點按視圖的一部分時,如何真正隱藏和顯示選項卡欄? (沒有按鈕,但屏幕的任何位置)

[英]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)
            }
        })
    }

}

我只能隱藏標簽欄,但是當您再次點擊時無法顯示它。 我試圖在堆棧溢出時尋找答案,但答案似乎僅在您使用按鈕或情節提要時才有效。

具有協議的通用解決方案,可在所有屏幕上使用

創建名為BaseViewController UIViewController並使它成為所有視圖控制器的基類

現在定義協議

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 Controller中

在視圖中將出現方法,添加以下代碼以顯示基於協議的隱藏

    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
    }

希望對您有所幫助

在類中有一個變量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
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM