我正在尝试隐藏第三个选项卡上的导航栏,但对于其余选项卡,它应该会显示出来。它在 iOS 14 中运行良好,但在 iOS 15 中,修饰符随机运行。一旦我开始在选项卡之间切换,导航栏也开始显示在第三个选项卡上。下面是我的 SwiftUI 文件代码
注意:这只是我为我的应用程序设置的更复杂的代码设置的简单表示。Xcode:12.5.1 设备:XR(iOS 15 测试版)
import SwiftUI
struct TestView: View {
@State var selectedTab: TabType = .home
var body: some View {
NavigationView {
TabView(selection: $selectedTab) {
ForEach(TabType.allCases, id: \.self) { tabType in
switch tabType {
case .profile:
ThirdView()
.navigationBarHidden(true)
.tag(tabType)
.tabItem {
Label(tabType.title, systemImage: "plus")
}
default:
SecondView()
.tag(tabType)
.tabItem {
Label(tabType.title, systemImage: "star")
}
}
}
}
}
}
}
struct SecondView: View {
var body: some View {
VStack {
Spacer()
Text("2nd view")
NavigationLink("3rd view", destination: ThirdView())
Spacer()
}
.background(Color.green)
}
}
struct ThirdView: View {
var body: some View {
ZStack {
Color.red
Text("3rd view")
}
}
}
enum TabType: CaseIterable {
case home
case discover
case profile
case more
var image: Image {
switch self {
case .home:
return Image(systemName: "plus")
case .discover:
return Image(systemName: "plus")
case .profile:
return Image(systemName: "plus")
case .more:
return Image(systemName: "plus")
}
}
var title: LocalizedStringKey {
switch self {
case .home:
return LocalizedStringKey("home")
case .discover:
return LocalizedStringKey("Discover")
case .profile:
return LocalizedStringKey("Me")
case .more:
return LocalizedStringKey("Shop")
}
}
}