我有一个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 中称为工作表?)?
如果我正确理解了您的目标,您可以考虑以下方法,基于使用事物包装视图的想法,它将目标视图呈现为工作表......
事情是这样的:
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
}
}
}
}
您的案例的用法是...
// New Listing
SheetPresenter(presentingSheet: $newListingPresented, content: NewListingView())
.tabItem {
VStack {
Image(systemName: (selection == 1 ? "plus.square.fill" : "plus.square"))
}
}
.tag(1)
如果您需要更改选项卡selection
在工作表中工作后,您可以在中传递一些附加参数SheetPresenter
并在工作表中使用它onDismiss: (() -> Void)?
打回来。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)