如何将多个导航链接嵌入到单个列表行中?

发布于 2025-01-15 08:31:07 字数 2579 浏览 5 评论 0原文

我正在尝试复制类似苹果提醒应用程序的东西,其中有一些“块”(今天、计划、全部和标记),但我所有的尝试都以 XCode 告诉我它遇到导航问题结束。我现在使用的是 XCode 13.2.1 和 iOS 15.2。

如何让每个块导航到自己的目的地并阻止应用程序因 NavigationLink 错误而阻塞?

这是我的代码:

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                Section(header: Text("Primary Section")) {
                    NavigationLink(destination: Text("First")) {
                        Label("First", systemImage: "1.circle")
                    }
                    Label("Second", systemImage: "2.circle")
                    Label("Third", systemImage: "3.circle")
                }
                Section(header: Text("Block Section")) {
                    VStack (spacing: 20) {
                        HStack (spacing: 20) {
                            Blocks(title: "1")
                            Blocks(title: "2")
                        }
                        HStack (spacing: 20) {
                            Blocks(title: "3")
                            Blocks(title: "4")
                        }
                    }
                    .foregroundColor(Color.blue)
                    .frame(height: 160)
                    .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
                    .listRowBackground(Color.clear)
                }
                Section(header: Text("Lower Section")) {
                    NavigationLink(destination: Text("Lower First")) {
                        Label("First", systemImage: "1.circle")
                    }
                    Label("Second", systemImage: "2.circle")
                    Label("Third", systemImage: "3.circle")
                    NavigationLink(destination: Text("Lower Fourth")){
                        Label("Fourth", systemImage: "4.circle")
                    }
                }
            }
            .listStyle(InsetGroupedListStyle())
            .navigationBarTitle(Text("Catalogue"), displayMode: .large)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct Blocks: View {
    let title: String
    var body: some View {
        ZStack {
            NavigationLink(
                destination: Text(title)) {
                EmptyView()
            }
            .opacity(0)
            RoundedRectangle(cornerRadius: 12, style: .continuous)
            Image(systemName: "\(title).circle.fill")
                .foregroundColor(.white)
                .imageScale(.large)
        }
    }
}```

I'm trying to replicate something like Apple's Reminders app where there are some "blocks" (Today, Scheduled, All and Flagged), but all my attempts end in XCode telling me its encountering navigation issues. I'm using XCode 13.2.1 and iOS 15.2 right now.

How can I get each bock to navigation to its own destination and stop the app from choking on the NavigationLink errors?

Here is my code:

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                Section(header: Text("Primary Section")) {
                    NavigationLink(destination: Text("First")) {
                        Label("First", systemImage: "1.circle")
                    }
                    Label("Second", systemImage: "2.circle")
                    Label("Third", systemImage: "3.circle")
                }
                Section(header: Text("Block Section")) {
                    VStack (spacing: 20) {
                        HStack (spacing: 20) {
                            Blocks(title: "1")
                            Blocks(title: "2")
                        }
                        HStack (spacing: 20) {
                            Blocks(title: "3")
                            Blocks(title: "4")
                        }
                    }
                    .foregroundColor(Color.blue)
                    .frame(height: 160)
                    .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
                    .listRowBackground(Color.clear)
                }
                Section(header: Text("Lower Section")) {
                    NavigationLink(destination: Text("Lower First")) {
                        Label("First", systemImage: "1.circle")
                    }
                    Label("Second", systemImage: "2.circle")
                    Label("Third", systemImage: "3.circle")
                    NavigationLink(destination: Text("Lower Fourth")){
                        Label("Fourth", systemImage: "4.circle")
                    }
                }
            }
            .listStyle(InsetGroupedListStyle())
            .navigationBarTitle(Text("Catalogue"), displayMode: .large)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct Blocks: View {
    let title: String
    var body: some View {
        ZStack {
            NavigationLink(
                destination: Text(title)) {
                EmptyView()
            }
            .opacity(0)
            RoundedRectangle(cornerRadius: 12, style: .continuous)
            Image(systemName: "\(title).circle.fill")
                .foregroundColor(.white)
                .imageScale(.large)
        }
    }
}```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文