如何在导航栏中同时有一个按钮和一个包含部分的列表?
下面是一个带有.navigationBarItems的代码
struct AllMatchesView: View {
@Environment(\.managedObjectContext) var moc
@State var events = EventData()
var body: some View {
NavigationView {
List{
ForEach(events.sections) { section in
Section(header: Text(section.title)) {
ForEach(section.matches) { match in
Text("Row")
}
}
.navigationBarTitle("Title")
.navigationBarItems(trailing:
NavigationLink(destination: AddMatchView().environment(\.managedObjectContext, self.moc)) {
Image(systemName: "plus")
.resizable()
.frame(width: 22, height: 22)
.padding(.horizontal)
}
.padding(.leading)
)
}
}.onAppear(){
self.events = EventData()
}
}
}
}无.navigationBarItems
struct AllMatchesView: View {
@Environment(\.managedObjectContext) var moc
@State var events = EventData()
var body: some View {
NavigationView {
List{
ForEach(events.sections) { section in
Section(header: Text(section.title)) {
ForEach(section.matches) { match in
Text("Row")
}
}
.navigationBarTitle("Title")
}
}.onAppear(){
self.events = EventData()
}
}
}
}用.navigationBarItems计算结果

没有.navigationBarItems的结果

发布于 2020-07-31 14:02:20
只需将这些修饰符从动态内容中移出,否则您将尝试为每个部分包括复制的条形项,这似乎使SwiftUI引擎变得疯狂。
var body: some View {
NavigationView {
List{
ForEach(events.sections) { section in
Section(header: Text(section.title)) {
ForEach(section.matches) { match in
Text("Row")
}
}
}
}
.navigationBarTitle("Title")
.navigationBarItems(trailing:
NavigationLink(destination: AddMatchView().environment(\.managedObjectContext, self.moc)) {
Image(systemName: "plus")
.resizable()
.frame(width: 22, height: 22)
.padding(.horizontal)
}
.padding(.leading)
)
.onAppear(){
self.events = EventData()
}
}
}https://stackoverflow.com/questions/63193141
复制相似问题