我的应用程序具有3个标签(读取,发现,配置文件)。在一个从“读取”视图导航到的特定视图中,我想隐藏选项卡视图并显示一个自定义工具栏/选项卡栏,其中将显示操作按钮(例如,保存等)。
我一直在Google搜索和搜索Stack Overflow已有2天,没有运气。这是我所见/尝试过的:
有人对工作代码有想法或示例吗?谢谢!
答案 0 :(得分:1)
我在以下代码中实现了两种解决方案:
enum MyTab: String {
case a, b, c, d
var view: AnyView {
switch self {
case .a, .b: return AnyView(ChildView(page: rawValue))
case .c, .d: return AnyView(SpecificView())
}
}
}
class ViewModel: ObservableObject {
@Published var tabs: [MyTab] = [.a, .b]
@Published var selectedTab: MyTab = .a
@Published var shouldShowTab = true
var customBinding: Binding<MyTab> {
Binding(get: {
self.selectedTab
}, set: {
if $0 == .c {
// save
} else if $0 == .d {
// like
}
self.selectedTab = $0
})
}
func updateTabs() {
tabs = [.c, .d]
}
}
struct ContentView: View {
@EnvironmentObject var viewModel: ViewModel
var body: some View {
Group {
if viewModel.shouldShowTab {
TabView(selection: viewModel.customBinding) {
ForEach(viewModel.tabs, id: \.self) { tab in
tab.view
.tabItem { Text(tab.rawValue) }
}
}
} else {
SpecificView()
}
}
}
}
struct ChildView: View {
@EnvironmentObject var viewModel: ViewModel
var page: String
var body: some View {
VStack(spacing: 24) {
Text("Page: \(page)")
Button(action: {
self.viewModel.shouldShowTab = false
}) {
Text("Go to Specific View")
}
Button(action: {
self.viewModel.updateTabs()
}) {
Text("or update tabs on the go")
}
}
}
}
struct SpecificView: View {
@EnvironmentObject var viewModel: ViewModel
var body: some View {
// Use whatever you'd like tab etc.
VStack(spacing: 24) {
Text("My Specific View")
Button(action: {
self.viewModel.shouldShowTab = true
}) {
Text("Go back to Tab View")
}
}
}
}
答案 1 :(得分:0)
您可以将新视图显示为Sheet
,这样您提到的特定视图就可以拥有自己的TabView
。
struct ContentView: View {
@State var presentView = false
var body: some View {
Button(action: {
self.presentView.toggle()
}) {
Text("Present View")
}
.sheet(isPresented: $presentView) {
TabView {
// content
}
}
}
}