我有一个关于 IOS 16 中新的 NavigationStack 的问题。我在 ContentView 中设置 navigationTitle 时遇到问题,我设置了 navigationTitle 但它对所有选项卡都是相同的。我可以以某种方式设置它,以便我可以为每个不同的选项卡编辑它吗?使用标签?非常感谢
struct ContentView: View {
@State var selection = 1
var body: some View {
NavigationStack {
TabView(selection: $selection) {
LaunchView()
.badge(2)
.tabItem {
Label("Received", systemImage: "tray.and.arrow.down.fill")
}
.tag(1)
DeView()
.tabItem {
Label("Sent", systemImage: "tray.and.arrow.up.fill")
}
.tag(2)
DeView()
.tabItem {
Label("Sent", systemImage: "tray.and.arrow.up.fill")
}
.tag(3)
}
.navigationTitle("test")
}
}
Run Code Online (Sandbox Code Playgroud)
}
您可以创建一个返回每个选择的标题的函数:
\nfunc title() -> String {\n if selection == 1 {\n return title\n }else if selection == 2 {\n return some Title\n }else if selection == 3 {\n return some other Title\n }\n} \nRun Code Online (Sandbox Code Playgroud)\n或者我个人最好的办法:Enums!
创建一个包含选项卡的枚举,然后为每个选项卡创建一个标题属性:
\nstruct ContentView: View {\n @State var selection = Tab.received\n var body: some View {\n NavigationStack {\n TabView(selection: $selection) {\n Text("hello")\n .badge(2)\n .tabItem {\n Label("Received", systemImage: "tray.and.arrow.down.fill")\n }\n .tag(Tab.received)\n Text("hello3")\n .tabItem {\n Label("Sent", systemImage: "tray.and.arrow.up.fill")\n }\n .tag(Tab.sent)\n Text("hello5")\n .tabItem {\n Label("Sent", systemImage: "tray.and.arrow.up.fill")\n }\n .tag(Tab.sennt)\n \n }.navigationTitle(selection.title)\n }\n }\n}\n\nenum Tab: Int {\n case received = 1\n case sent = 2\n case sennt = 3\n var title: String {\n switch self {\n case .received:\n return "Hello"\n case .sent:\n return "Hello World"\n case .sennt:\n return "Hello, World!"\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n另外,它\xe2\x80\x99s 比 s 更容易使用Int。
编辑:隐藏 TabBar DeviceView:
struct Test: View {\n @State var selection = 1\n @State var devicePresented = false\n var body: some View {\n NavigationStack {\n //Content\n .navigationDestination(isPresented: $devicePresented) {//present DeviceView when devicePresented is true\n DeviceView()\n }\n }\n }\n}\n\nstruct SettingsView: View {\n @Binding var devicePresented: Bool\n var body: some View {\n List {\n Button(action: {\n devicePresented.toggle()\n }) {\n Text("Go to device")\n }\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
3983 次 |
| 最近记录: |