Files
JetsonMediaIOS/Jetson Media/ui/MainView.swift
2025-08-17 22:08:25 +08:00

125 lines
4.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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. SwiftUIUIKit
struct TabBarContainerView: UIViewControllerRepresentable {
@Binding var isTabBarHidden: Bool
init(isTabBarHidden: Binding<Bool>) {
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) //
}
}