看起来 Navigation + TabView + Sheet 在 iOS 15 中被破坏了。
当我这样做时: ContentView -> DetailView -> Bottom Sheet
当底部工作表出现时,Detail
视图会自动从堆栈中弹出:
https ://www.youtube.com/watch?v=gguLptAx0l4
我希望Detail
即使出现底部表格,视图也会保留在那里。有谁知道为什么会发生这种情况以及如何解决它?
这是我的示例代码:
import Combine
import SwiftUI
import RealmSwift
struct ContentView: View {
var body: some View {
NavigationView {
TabView {
TabItemView(num: 1)
.tabItem {
Text("One")
}
TabItemView(num: 2)
.tabItem {
Text("Two")
}
}
}
}
}
struct TabItemView: View {
private let num: Int
init(num: Int) {
self.num = num
}
var body: some View {
NavigationLink(destination: DetailView(text: "Detail View \(num)")) {
Text("Go to Detail View")
}
}
}
struct DetailView: View {
@State private var showingSheet = false
private let text: String
init(text: String) {
self.text = text
}
var body: some View {
Button("Open Sheet") {
showingSheet.toggle()
}.sheet(isPresented: $showingSheet) {
Text("Sheet Text")
}
}
}
这适用于 iOS 14 顺便说一句
更新 1:
尝试了@Sebastian 提出的NavigationView
将TabView
. 虽然这修复了导航错误,但它从根本上改变了行为(我不想在 中显示选项卡DetailView
)。
还尝试了他使用Introspect设置目的地navigationController.hidesBottomBarWhenPushed = true
的建议NavigationLink
,但这并没有做任何事情:
struct ContentView: View {
var body: some View {
TabView {
NavigationView {
TabItemView(num: 1)
}.tabItem {
Text("One")
}
NavigationView {
TabItemView(num: 2)
}.tabItem {
Text("Two")
}
}
}
}
struct TabItemView: View {
private let num: Int
init(num: Int) {
self.num = num
}
var body: some View {
NavigationLink(destination: DetailView(text: "Detail View \(num)").introspectNavigationController { navigationController in
navigationController.hidesBottomBarWhenPushed = true
}) {
Text("Go to Detail View")
}
}
}
struct DetailView: View {
@State private var showingSheet = false
private let text: String
init(text: String) {
self.text = text
}
var body: some View {
Button("Open Sheet") {
showingSheet.toggle()
}.sheet(isPresented: $showingSheet) {
Text("Sheet Text")
}
}
}