我试图隐藏我的TabView
当我在我的视图中推送一个新视图时NavigationView
但目前看来没有办法做到这一点(我在互联网上看到了很多东西,但似乎没有什么适合我)?
默认情况下,我的代码如下所示:
struct ContentView: View {
var body: some View {
TabView {
NavigationView {
view1
}
.tabItem {
Image(systemName: "house.fill")
Text("Home")
}
NavigationView {
view2
}
.tabItem {
Image(systemName: "bookmark.circle.fill")
Text("Bookmark")
}
}
.accentColor(.red)
}
private var view1: some View {
List {
NavigationLink {
DetailsView()
} label: {
Text("View 1")
}
}
.navigationTitle("View 1")
}
private var view2: some View {
List {
NavigationLink {
DetailsView()
} label: {
Text("View 2")
}
}
.navigationTitle("View 2")
}
}
struct DetailsView: View {
var body: some View {
EmptyView()
}
}
但是,我没有任何办法来隐藏我的TabView
,所以我在我的ContentView
:
var body: some View {
NavigationView {
TabView {
view1
.tabItem {
Image(systemName: "house.fill")
Text("Home")
}
.navigationTitle(Text("title"))
view2
.tabItem {
Image(systemName: "bookmark.circle.fill")
Text("Bookmark")
}
.navigationTitle(Text("title"))
}
.accentColor(.red)
}
}
现在,隐藏推送正在工作,但它会导致导航栏中出现一些故障,并且我无法设置多个navigationTitle
(每个视图一个)就像我以前所做的那样;我只能设置一个navigationTitle
为了NavigationView
.
为了解决NavigationView
标题,我找到了一个解决方法,使用@State
变量,但它在导航栏上仍然存在这个故障:有时导航视图背景正常工作,有时不工作,有时标题和内容之间有间距(例如在“书签”选项卡中):
我究竟做错了什么?
对于这个问题是否有任何解决方案(hidesBottomBarWhenPushed
+ 导航栏故障)?
一种可能的方法是放置NavigationView
and TabView
in ZStack
(第一个在第二个之上)并单独使用两者,使它们与选择同步。
这里唯一的挑战是计算顶视图的高度以使选项卡栏可见,一旦我们实现了这一点,选项卡栏的可见性就可以根据链接激活来打开/关闭。
使用 Xcode 14 / iOS 16 进行测试
主要部分:
ZStack {
TabView(selection: $selection) {
GeometryReader { gp in
Color.clear.onAppear {
height = gp.size.height
}.onChange(of: gp.size) { _ in
height = gp.size.height
}
}
.tabItem {
Image(systemName: "house.fill")
Text("Home")
}.tag(1)
Color.clear
.tabItem {
Image(systemName: "bookmark.circle.fill")
Text("Bookmark")
}.tag(2)
}
.accentColor(.red)
NavigationView {
if selection == 1 {
view1
} else {
view2
}
}
.frame(maxHeight: tabBarVisible ? height : .infinity)
.frame(maxHeight: .infinity, alignment: .top)
.onChange(of: linkedTag) { tag in
tabBarVisible = tag == nil
}
GitHub 上的测试代码
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)