在根据条件显示的两个视图上使用相同的 `navigationBarTitle` 和 `navigationBarItems`

Use same `navigationBarTitle` and `navigationBarItems` on two views which are shown based on condition

我有一个视图显示 Text 视图以显示帮助文本,供用户点击加号图标以添加组。添加组后,它会显示 List 视图。要显示导航栏,我需要在 TextList 视图上调用 navigationBarTitlenavigationBarItems。下面是我的代码片段。

import SwiftUI

struct Home:View {
    @EnvironmentObject var dataStore:DataStore
    var body: some View {
        NavigationView {
            if dataStore.groups.isEmpty {
                Text("Tap on + icon to add group.")
                    .font(.caption)
                    .multilineTextAlignment(.center)
                    .padding()
                    .foregroundColor(.gray)
                    .navigationBarTitle(Text("My App Name"), displayMode: .automatic)
                    .navigationBarItems(
                        trailing:
                            NavigationLink(
                                destination:
                                    CreateGroup(),
                                label: {
                                    Image(systemName: "plus")
                                        .foregroundColor(Color.blue)
                                })
                    )
            } else {
                List(dataStore.groups) { groupElement in
                    GroupRow(group: groupElement)
                }
                .navigationBarTitle(Text("My App Name"), displayMode: .automatic)
                .navigationBarItems(
                    trailing:
                        NavigationLink(
                            destination:
                                CreateGroup(),
                            label: {
                                Image(systemName: "plus")
                                    .foregroundColor(Color.blue)
                            })
                )
            }
        }
    }
}

有没有办法只调用 navigationBarTitlenavigationBarItems 一次,而不是同时调用 TextList 视图?

Is there a way to call navigationBarTitle and navigationBarItems only once rather than calling on both Text and List views?

是的,您可以将条件包装到任何容器中,例如 Group 或 xStack:

struct Home:View {
    @EnvironmentObject var dataStore:DataStore
    var body: some View {
        NavigationView {
            Group {
                if dataStore.groups.isEmpty {
                    Text("Tap on + icon to add group.")
                        .font(.caption)
                        .multilineTextAlignment(.center)
                        .padding()
                        .foregroundColor(.gray)
                } else {
                    List(dataStore.groups) { groupElement in
                        GroupRow(group: groupElement)
                    }
                }
            }
            .navigationBarTitle(Text("My App Name"), displayMode: .automatic)
            .navigationBarItems(
                trailing:
                    NavigationLink(
                        destination:
                            CreateGroup(),
                        label: {
                            Image(systemName: "plus")
                                .foregroundColor(Color.blue)
                        })
            )

        }
    }
}