选择图片分享功能

This commit is contained in:
2025-08-17 22:08:25 +08:00
parent e6afca5ba6
commit d80289fe73
19 changed files with 1402 additions and 110 deletions

View File

@@ -0,0 +1,124 @@
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) //
}
}