如何在 SwiftUI 中交换 TabView 和 BottomToolbar

时间:2021-06-24 13:45:23

标签: swift swiftui toolbar swiftui-list swiftui-tabview

我有这 2 个视图,当点击 EditButton 时,我想交换其中的两个(TestView/BottomToolbarBottomView/TabView)。

  • TestView/BottomToolbar 可见
  • BottomView/TabView 隐形

我可以做这样的事情吗?如果是,如何?

struct BottomView: View {
    @State private var tabSelection = 0
    var body: some View {
        TabView(selection: $tabSelection) {
            TestView().tabItem {
                Image(systemName: "tray")
            }.tag(0)
            AnotherView().tabItem {
                Image(systemName: "house")
            }.tag(1)
        }
    }
}

struct TestView: View {
    @State private var list = [1,2,3,4,5]
    @State private var selection = Set<Int>()
    @State var editMode: EditMode = .inactive
    
    var body: some View {
        NavigationView {
            List(selection: $selection) {
                ForEach(list, id: \.self) { item in
                    Text("\(item)")
                }
            }
            .toolbar {
                ToolbarItemGroup(placement: .navigationBarLeading) {
                    EditButton()
                }
                ToolbarItemGroup(placement: .bottomBar) {
                    if editMode == .active {
                        Button("Approve") { }
                        Spacer()
                        Button("Reject") { }
                    }
                }
            }
            .environment(\.editMode, $editMode)
        }
    }
}

0 个答案:

没有答案