1、滑动的时候先获取底部tab,利用UIView.animate设置滑动退出的动画效果。
2、滑动的时候获取顶部的导航栏,需要根据自己的项目来,有的只是系统的导航栏,有的是自定义的导航栏。
3、注意:在隐藏底部tab的时候需要设置viewController的高度来适应tab的显示与隐藏,否则在隐藏tab之后底部会出现一个黑色区域。
4、UIView.animate数值方向的动画效果,主要根据自己的项目的布局的高度设置y坐标即可。
5、具体实现代码如下。
var complete: Bool = true
func hideTabNav(hidden:Bool, animated: Bool){
guard let tabBar = self.tabBarController?.tabBar else { return; }
if tabBar.isHidden == hidden{ return }
let frame = tabBar.frame
let offset = hidden ? frame.size.height : -frame.size.height
let duration:TimeInterval = (animated ? 0.5 : 0.0)
tabBar.isHidden = false
self.complete = false
//self.viewController根据自己的项目获取top viewController,并且需要根据显示与否设置 内容高度,否则隐藏tab之后会出现底部黑边的情况
self.viewController?.view.frame = CGRect(x: 0, y: 0, width:
UIScreen.mf_ScreenWidth, height: UIScreen.mf_ScreenHeight)
//导航栏,根据自己的项目获取到顶部导航栏的view
self.homeViewController?.topHidden = true
let customNavigationView = self.homeViewController?.customNavigationView
customNavigationView?.isHidden = false
let cnFrame = customNavigationView?.frame
let cnOffset = hidden ? -(cnFrame?.height ?? 0) : (cnFrame?.height ?? 0)
let bgView = self.homeViewController?.bgView
bgView?.isHidden = false
let bgFrame = bgView?.frame
let bgOffset = hidden ? -(bgFrame?.height ?? 0) : (bgFrame?.height ?? 0)
UIView.animate(withDuration: duration, animations: {
tabBar.frame = frame.offsetBy(dx: 0, dy: offset)
customNavigationView?.frame = (cnFrame?.offsetBy(dx: 0, dy: cnOffset))!
bgView?.frame = (bgFrame?.offsetBy(dx: 0, dy: bgOffset))!
}, completion: { (true) in
tabBar.isHidden = hidden
customNavigationView?.isHidden = hidden
bgView?.isHidden = hidden
self.complete = true
if hidden {
self.homeViewController?.view.frame = CGRect(x: 0, y: 0, width: UIScreen.mf_ScreenWidth, height: UIScreen.mf_ScreenHeight)
} else {
self.homeViewController?.view.frame = CGRect(x: 0, y: 0, width: UIScreen.mf_ScreenWidth, height: UIScreen.mf_ScreenHeight- tabBar.frame.height)
}
})
}