集成KYDrawerController时TabBar消失的问题排查与解决求助
解决KYDrawerController集成后TabBar消失的问题
问题原因
你当前的代码犯了一个典型的结构配置错误——你把TabBarController的子页面(单个ViewController)直接设为了KYDrawerController的mainViewController,这相当于直接替换掉了原本的TabBarController根容器,导致TabBar失去了它的宿主载体,自然会消失且无法恢复。正确的逻辑应该是将完整的TabBarController作为KYDrawerController的主视图容器,让抽屉视图覆盖在TabBar的上层,而不是替换掉TabBar的容器结构。
解决方案
根据你的项目结构,调整代码逻辑如下:
1. 确认Storyboard配置
首先确保你的Storyboard中存在一个UITabBarController,并且给它设置了唯一标识符(比如"MainTabBarController"),它的子视图控制器就是你原本的各个业务页面。
2. 修改代码逻辑
将实例化单个ViewController改为实例化TabBarController,作为KYDrawerController的主视图:
let appDel = UIApplication.shared.delegate as! AppDelegate // 实例化完整的TabBarController作为主视图容器 let mainTabBarVC = self.storyboard?.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController let menuVC = self.storyboard?.instantiateViewController(withIdentifier: "DrawerController") // 配置KYDrawerController的核心参数 appDel.drawerController.mainViewController = mainTabBarVC appDel.drawerController.drawerViewController = menuVC appDel.drawerController.screenEdgePanGestureEnabled = false // 更新根视图并打开抽屉 appDel.window?.rootViewController = appDel.drawerController appDel.window?.makeKeyAndVisible() appDel.drawerController.setDrawerState(.opened, animated: true)
3. 替代方案(无需重新实例化TabBarController)
如果当前视图控制器本身就是TabBarController的子页面,可以直接获取当前的TabBarController作为主视图:
let appDel = UIApplication.shared.delegate as! AppDelegate // 获取当前所在的TabBarController guard let mainTabBarVC = self.tabBarController else { print("无法获取当前TabBarController") return } let menuVC = self.storyboard?.instantiateViewController(withIdentifier: "DrawerController") appDel.drawerController.mainViewController = mainTabBarVC appDel.drawerController.drawerViewController = menuVC appDel.drawerController.screenEdgePanGestureEnabled = false appDel.window?.rootViewController = appDel.drawerController appDel.window?.makeKeyAndVisible() appDel.drawerController.setDrawerState(.opened, animated: true)
额外注意事项
- 确保使用的是最新版本的KYDrawerController,避免旧版本的兼容性问题
- 如果设置后TabBar仍未显示,可以尝试在配置完成后手动调用
mainTabBarVC.tabBar.isHidden = false强制显示 - 检查你的DrawerController是否有布局上的遮挡,确保它不会覆盖到TabBar的显示区域
内容的提问来源于stack exchange,提问作者spectre_d2
相关产品推荐
相关产品推荐

