简体   繁体   English

SwiftUI 通过 TabView 模态显示视图?

[英]SwiftUI Present View Modally via TabView?

I have a TabView set up as follows:我有一个TabView设置如下:

struct ContentView: View {
    @State private var selection = 0
    @State var newListingPresented = false

    var body: some View {
        TabView(selection: $selection){

            // Browse
            BrowseView()
                .tabItem {
                    VStack {
                        Image(systemName: (selection == 0 ? "square.grid.2x2.fill" : "square.grid.2x2"))
                    }
                }
                .tag(0)


            // New Listing
            NewListingView()
                .tabItem {
                    VStack {
                        Image(systemName: (selection == 1 ? "plus.square.fill" : "plus.square"))
                    }
                }
                .tag(1)

            // Bag
            BagView()
                .tabItem {
                    VStack {
                        Image(systemName: (selection == 2 ? "bag.fill" : "bag"))
                    }
                }
                .tag(2)

            // Profile
            ProfileView()
                .tabItem {
                    VStack {
                        Image(systemName: (selection == 3 ? "person.crop.square.fill" : "person.crop.square"))
                    }
                }
                .tag(3)
        }.edgesIgnoringSafeArea(.top)
    }
}

Question: How would I get the "New Listing" tab to present NewListingView modally (called sheet in SwiftUI?) when tapped?问题:点击时如何让“新列表”选项卡以模态方式呈现NewListingView (在 SwiftUI 中称为工作表?)?

If I correctly understood your goal you could consider the following approach, based on idea of using thing wrapper view which will present target view as a sheet...如果我正确理解了您的目标,您可以考虑以下方法,基于使用事物包装器视图的想法,它将目标视图呈现为一张纸......

Here it goes:它是这样的:

struct SheetPresenter<Content>: View where Content: View {
    @Binding var presentingSheet: Bool
    var content: Content
    var body: some View {
        Text("")
            .sheet(isPresented: self.$presentingSheet, content: { self.content })
            .onAppear {
                DispatchQueue.main.async {
                    self.presentingSheet = true
                }
            }
    }
}

and usage for your case is...你的情况的用法是......

// New Listing
    SheetPresenter(presentingSheet: $newListingPresented, content: NewListingView())
    .tabItem {
        VStack {
            Image(systemName: (selection == 1 ? "plus.square.fill" : "plus.square"))
        }
    }
    .tag(1)

If you will need to to change tab selection after work in sheet you could pass some additional argument in SheetPresenter and use it in sheet's onDismiss: (() -> Void)?如果您需要在工作表中更改选项卡selection ,您可以在SheetPresenter中传递一些额外的参数并在工作表的onDismiss: (() -> Void)? callback.打回来。

backup 备份

I think you're looking for this solution.我想你正在寻找这个解决方案。 You make empty tabItem ( Text(" ") ), set Image at needed position and use .onTapGesture .您制作空的 tabItem ( Text(" ") ),将Image设置在需要的位置并使用.onTapGesture In that answer I'm just showing how to present ActionSheet .在那个答案中,我只是展示了如何呈现ActionSheet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM