List Sections do not work correctly if .navigationBarItems is present

故事扮演 提交于 2021-01-29 06:36:40

问题


How to have both a button in navigation bar and a list with sections?

Here is a code with .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()
            }
        }
    }
}

Without .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()
            }
        }
    }
}

Result with .navigationBarItems:

pic 1

Result without .navigationBarItems:

pic 2


回答1:


Just move those modifiers out of dynamic content, otherwise you try to include duplicated bar items for every section, that seems makes SwiftUI engine crazy.

    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/list-sections-do-not-work-correctly-if-navigationbaritems-is-present

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!