SwiftUI 通过 TabView 模态呈现视图?
SwiftUI Present View Modally via 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)
}
}
问题:
如何让 "New Listing" 选项卡在点击时以模态方式呈现 NewListingView
(在 SwiftUI 中称为 sheet?)?
我认为您正在寻找 解决方案。您创建空的 tabItem (Text(" ")
),在需要的位置设置 Image
并使用 .onTapGesture
。在那个答案中,我只是展示了如何呈现 ActionSheet
.
如果我正确理解了您的目标,您可以考虑以下方法,基于使用事物包装器视图的想法,它将目标视图呈现为 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
}
}
}
}
您的情况的用法是...
// New Listing
SheetPresenter(presentingSheet: $newListingPresented, content: NewListingView())
.tabItem {
VStack {
Image(systemName: (selection == 1 ? "plus.square.fill" : "plus.square"))
}
}
.tag(1)
如果您需要在 sheet 工作后更改选项卡 selection
,您可以在 SheetPresenter
中传递一些额外的参数并在 sheet 的 onDismiss: (() -> Void)?
回调。
我有一个 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)
}
}
问题:
如何让 "New Listing" 选项卡在点击时以模态方式呈现 NewListingView
(在 SwiftUI 中称为 sheet?)?
我认为您正在寻找 Text(" ")
),在需要的位置设置 Image
并使用 .onTapGesture
。在那个答案中,我只是展示了如何呈现 ActionSheet
.
如果我正确理解了您的目标,您可以考虑以下方法,基于使用事物包装器视图的想法,它将目标视图呈现为 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
}
}
}
}
您的情况的用法是...
// New Listing
SheetPresenter(presentingSheet: $newListingPresented, content: NewListingView())
.tabItem {
VStack {
Image(systemName: (selection == 1 ? "plus.square.fill" : "plus.square"))
}
}
.tag(1)
如果您需要在 sheet 工作后更改选项卡 selection
,您可以在 SheetPresenter
中传递一些额外的参数并在 sheet 的 onDismiss: (() -> Void)?
回调。