import SwiftUI import UIKit // 1. 状态管理类 - 控制底部导航栏显示状态 class TabBarState: ObservableObject { @Published var isTabBarHidden = false // 控制底部导航栏是否隐藏 } // 扩展状态管理类,提供便捷方法 extension TabBarState { func hideTabBar() { isTabBarHidden = true } func showTabBar() { isTabBarHidden = false } func toggleTabBar() { isTabBarHidden.toggle() } } // 2. 自定义TabBar控制器(支持全屏显示) class CustomTabBarController: UITabBarController { var shouldHideTabBar: Bool = false { didSet { updateTabBarVisibility() } } override func viewDidLoad() { super.viewDidLoad() // 配置控制器支持全屏 edgesForExtendedLayout = .all // 允许内容延伸到导航栏/状态栏下方 extendedLayoutIncludesOpaqueBars = true // 包含不透明的导航栏 automaticallyAdjustsScrollViewInsets = false // 禁止自动调整滚动视图Insets } private func updateTabBarVisibility() { UIView.animate(withDuration: 0.3) { self.tabBar.isHidden = self.shouldHideTabBar // 调整安全区域:全屏时移除底部额外内边距 self.viewControllers?.forEach { vc in vc.additionalSafeAreaInsets.bottom = self.shouldHideTabBar ? 0 : 0 // 强制子视图刷新布局 vc.view.setNeedsLayout() } } } } // 3. SwiftUI与UIKit的桥接视图(支持全屏) struct TabBarContainerView: UIViewControllerRepresentable { @Binding var isTabBarHidden: Bool init(isTabBarHidden: Binding) { self._isTabBarHidden = isTabBarHidden } func makeCoordinator() -> Coordinator { Coordinator(self) } func makeUIViewController(context: Context) -> CustomTabBarController { let tabBarController = CustomTabBarController() // 配置搜索标签页(添加全屏修饰符) let searchView = SearchView() .edgesIgnoringSafeArea(.all) // 忽略安全区域 let searchVC = UIHostingController(rootView: searchView) searchVC.tabBarItem = UITabBarItem( title: "搜索", image: UIImage(systemName: "magnifyingglass"), tag: 0 ) searchVC.edgesForExtendedLayout = .all // 配置排行榜标签页(添加全屏修饰符) let rankingsView = RankingsView() .edgesIgnoringSafeArea(.all) let rankingsVC = UIHostingController(rootView: rankingsView) rankingsVC.tabBarItem = UITabBarItem( title: "排行榜", image: UIImage(systemName: "chart.bar"), tag: 1 ) rankingsVC.edgesForExtendedLayout = .all // 配置我的标签页(添加全屏修饰符) let profileView = ProfileView() .edgesIgnoringSafeArea(.all) let profileVC = UIHostingController(rootView: profileView) profileVC.tabBarItem = UITabBarItem( title: "我的", image: UIImage(systemName: "person"), tag: 2 ) profileVC.edgesForExtendedLayout = .all tabBarController.viewControllers = [searchVC, rankingsVC, profileVC] return tabBarController } func updateUIViewController(_ uiViewController: CustomTabBarController, context: Context) { uiViewController.shouldHideTabBar = isTabBarHidden } class Coordinator: NSObject { var parent: TabBarContainerView init(_ parent: TabBarContainerView) { self.parent = parent } } } // 4. 主视图(支持全屏) struct MainView: View { @EnvironmentObject var tabState: TabBarState var body: some View { TabBarContainerView(isTabBarHidden: $tabState.isTabBarHidden) .navigationTitle("漫画列表") .navigationBarTitleDisplayMode(.inline) .toolbarBackground(.hidden, for: .navigationBar) .edgesIgnoringSafeArea(.all) // 主视图忽略安全区域 } }