我有一个视图,它显示Text
视图以显示帮助文本,供用户点击加号图标以添加组。添加组后,它会显示List
视图。要显示导航栏,我需要同时调用和navigationBarTitle
查看。下面是我的代码片段。navigationBarItems
Text
List
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)
})
)
}
}
}
}
有没有办法只调用一次而不是同时调用navigationBarTitle
和视图?navigationBarItems
Text
List