gpt4 book ai didi

ios - 在 SwiftUI 中使用包装的 UIKit 选项卡 View 在按钮按下时切换选项卡

转载 作者:行者123 更新时间:2023-12-01 21:41:16 24 4
gpt4 key购买 nike

当按下 SocialView() 上的按钮时,我试图切换到另一个选项卡(比如 WorkOutView())。但是,我一直在 SwiftUI 中使用 UIKit tabview 的包装结构。
包装器:

struct UIKitStyleTabView: View {
var viewControllers: [HostingController<AnyView>]

//@State var switchTab = 0

struct Tab {
var view: AnyView
var barItem: UITabBarItem

init<V: View>(view: V, barItem: UITabBarItem) {
self.view = AnyView(view)
self.barItem = barItem
}
}

init(_ tabs: [Tab]) {
self.viewControllers = tabs.map {
let host = HostingController(rootView: $0.view)
host.tabBarItem = $0.barItem
return host
}
}

var body: some View {
TabBarController(controllers: viewControllers, switchTab: $switchTab)
.edgesIgnoringSafeArea(.all)
}
}

struct TabBarController: UIViewControllerRepresentable {
var controllers: [UIViewController]

@Binding var switchTav: Int

func makeUIViewController(context: Context) -> UITabBarController {
let tabBarController = UITabBarController()
tabBarController.viewControllers = controllers
return tabBarController
}

func updateUIViewController(_ uiViewController: UITabBarController, context: Context) {
//uiViewController.selectedIndex = switchTab
}
}
我这样设置:
struct MainView : View {
//@Binding var switchTab: Int

let tabBarSymbolConfig = UIImage.SymbolConfiguration(scale: .medium)

var body : some View {

UIKitStyleTabView([
UIKitStyleTabView.Tab(view: SocialView(switchTab: $switchTab).accentColor(.white), barItem: UITabBarItem(title: "Social", image: UIImage(systemName: "person.3.fill", withConfiguration: tabBarSymbolConfig)!.withBaselineOffset(fromBottom: 4.5), selectedImage: nil)
), UIKitStyleTabView.Tab(view: SearchView().accentColor(.white), barItem: UITabBarItem(title: "Search", image: UIImage(systemName: "magnifyingglass", withConfiguration: tabBarSymbolConfig)!.withBaselineOffset(fromBottom: 4.5), selectedImage: nil)
), UIKitStyleTabView.Tab(view: WorkoutsView().accentColor(.white),barItem: UITabBarItem(title: "Workouts", image: UIImage(systemName: "doc.on.clipboard.fill", withConfiguration: tabBarSymbolConfig)!.withBaselineOffset(fromBottom: 4.5), selectedImage: nil)
), UIKitStyleTabView.Tab(view: ExercisesView().accentColor(.white), barItem: UITabBarItem(title: "Exercises", image: UIImage(systemName: "sportscourt", withConfiguration: tabBarSymbolConfig)!.withBaselineOffset(fromBottom: 4.5), selectedImage: nil)
), UIKitStyleTabView.Tab(view: AccountView().accentColor(.white), barItem: UITabBarItem(title: "Me", image: UIImage(systemName: "person.crop.circle", withConfiguration: tabBarSymbolConfig)!.withBaselineOffset(fromBottom: 4.5), selectedImage: nil)
)
], switchTab: $switchTab).accentColor(.navIconColor)

我试图在连接这些 View 的任何地方插入一个状态/绑定(bind)变量,这样我就可以在函数 updateUIViewController 中使用它,但这并没有真正奏效,因为我最终不得不在 SceneDelegate 中插入一个绑定(bind)。
任何建议如何实现这一目标?

最佳答案

这是更正和工作的变体(由于缺少许多组件而被复制,所以适应回来是你的事)。
使用 Xcode 11.4/iOS 13.4 测试
demo
完整的模块代码:

struct UIKitStyleTabView: View {
var viewControllers: [UIHostingController<AnyView>]

@Binding var switchTab: Int

struct Tab {
var view: AnyView
var barItem: UITabBarItem

init<V: View>(view: V, barItem: UITabBarItem) {
self.view = AnyView(view)
self.barItem = barItem
}
}

init(_ tabs: [Tab], switchTab: Binding<Int>) {
self.viewControllers = tabs.map {
let host = UIHostingController(rootView: $0.view)
host.tabBarItem = $0.barItem
return host
}
self._switchTab = switchTab
}

var body: some View {
TabBarController(controllers: viewControllers, switchTab: $switchTab)
.edgesIgnoringSafeArea(.all)
}
}

struct TabBarController: UIViewControllerRepresentable {
var controllers: [UIViewController]

@Binding var switchTab: Int

func makeUIViewController(context: Context) -> UITabBarController {
let tabBarController = UITabBarController()
tabBarController.viewControllers = controllers
tabBarController.delegate = context.coordinator
return tabBarController
}

func updateUIViewController(_ uiViewController: UITabBarController, context: Context) {
uiViewController.selectedIndex = switchTab
}

func makeCoordinator() -> Coordinator {
Coordinator(self)
}

class Coordinator: NSObject, UITabBarControllerDelegate {
let owner: TabBarController
init(_ owner: TabBarController) {
self.owner = owner
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
owner.switchTab = tabBarController.selectedIndex
}
}
}

struct TabsMainView : View {
@State var switchTab: Int = 0

let tabBarSymbolConfig = UIImage.SymbolConfiguration(scale: .medium)

var body : some View {

UIKitStyleTabView([
UIKitStyleTabView.Tab(view: SocialView(switchTab: $switchTab).accentColor(.white), barItem: UITabBarItem(title: "Social", image: UIImage(systemName: "person.3.fill", withConfiguration: tabBarSymbolConfig)!.withBaselineOffset(fromBottom: 4.5), selectedImage: nil)
), UIKitStyleTabView.Tab(view: Text("SearchView").accentColor(.white), barItem: UITabBarItem(title: "Search", image: UIImage(systemName: "magnifyingglass", withConfiguration: tabBarSymbolConfig)!.withBaselineOffset(fromBottom: 4.5), selectedImage: nil)
), UIKitStyleTabView.Tab(view: Text("WorkoutsView").accentColor(.white),barItem: UITabBarItem(title: "Workouts", image: UIImage(systemName: "doc.on.clipboard.fill", withConfiguration: tabBarSymbolConfig)!.withBaselineOffset(fromBottom: 4.5), selectedImage: nil)
), UIKitStyleTabView.Tab(view: Text("ExercisesView").accentColor(.white), barItem: UITabBarItem(title: "Exercises", image: UIImage(systemName: "sportscourt", withConfiguration: tabBarSymbolConfig)!.withBaselineOffset(fromBottom: 4.5), selectedImage: nil)
), UIKitStyleTabView.Tab(view: Text("AccountView").accentColor(.white), barItem: UITabBarItem(title: "Me", image: UIImage(systemName: "person.crop.circle", withConfiguration: tabBarSymbolConfig)!.withBaselineOffset(fromBottom: 4.5), selectedImage: nil)
)
], switchTab: $switchTab)//.accentColor(.navIconColor)
}
}

struct SocialView: View {
@Binding var switchTab: Int

var body: some View {
Button("Go Search") { self.switchTab = 1 }
}
}

struct TabsMainView_Previews: PreviewProvider {
static var previews: some View {
TabsMainView().colorScheme(.dark)
}
}

关于ios - 在 SwiftUI 中使用包装的 UIKit 选项卡 View 在按钮按下时切换选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62522021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com