先看看效果图吧,看是否是你们想要的效果:
其实思路很简单,首先在继承自UITabbarController的MainTabbarController中加入一个方法,用动画的方式改变tabbar的y坐标.
func hideTabbar(hidden: Bool) {
UIView.animate(withDuration: 0.2) {
if hidden {
var frame = self.tabBar.frame
frame.origin.y = ScreenHeight
self.tabBar.frame = frame
} else {
var frame = self.tabBar.frame
frame.origin.y = ScreenHeight - KTabBarHeight
self.tabBar.frame = frame
}
}
}
其中KTabBarHeight代表tabbar高度用以适配iPhone X.
然后在继承自UINavigationController的基类中做push操作,当从首页往下一级页面push的时候调用tabbar的隐藏动画,如下:
override func pushViewController(_ viewController: UIViewController, animated: Bool){
if (self.viewControllers.count == 1) {
let tabbarVC : MainTabBarController = self.tabBarController as! MainTabBarController
tabbarVC.hideTabbar(hidden: true)
}
super .pushViewController(viewController, animated: animated)
}
最后在继承自UIViewController的基类中重写viewDidAppear方法,当首页已经出现后调用tabbar的显示动画.如下:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let tabbarVC : MainTabBarController = self.tabBarController as! MainTabBarController
if (self.navigationController?.viewControllers.count)! <= 1 {
tabbarVC.hideTabbar(hidden: false)
}
}
大致思路就是这样,如果有什么更好的方法欢迎留言.