如果 .navigationBarItems 存在,则列表部分无法正常工作 [英] List Sections do not work correctly if .navigationBarItems is present

查看:39
本文介绍了如果 .navigationBarItems 存在,则列表部分无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在导航栏中同时拥有一个按钮和一个包含部分的列表?

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

这是一个带有 .navigationBarItems 的代码:

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

没有.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的结果:

推荐答案

只需将这些修饰符从动态内容中移出,否则您尝试为每个部分包含重复的栏项,这似乎让 SwiftUI 引擎发疯.

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

这篇关于如果 .navigationBarItems 存在,则列表部分无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆