SwiftUI当前视图通过TabView进行模态显示吗? [英] SwiftUI Present View Modally via TabView?

查看:184
本文介绍了SwiftUI当前视图通过TabView进行模态显示吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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)
    }
}

问题: 轻按时,如何获取新列表"选项卡以模态显示NewListingView(在SwiftUI中称为工作表?)?

Question: How would I get the "New Listing" tab to present NewListingView modally (called sheet in SwiftUI?) when tapped?

推荐答案

如果我正确理解了您的目标,则可以基于以下方法来考虑以下方法:使用东西包装视图将目标视图显示为表格...

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...

在这里:

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)

如果您需要在工作表中更改选项卡selection,则可以在SheetPresenter中传递一些附加参数,并将其用于工作表的onDismiss: (() -> Void)?回调中.

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)? callback.

这篇关于SwiftUI当前视图通过TabView进行模态显示吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆