I'm Just a Bill
I'm Just a Bill

Reputation: 1

Some NavigationLink are inaccessible

My SwiftUI TVOS app has two sets of NavigationLink. When both sets are present (not commented out), only one set is accessible to tap on. If I comment out one or the other set, the remaining NavigationLink is accessible to tap on and functions properly.

How can both sets of NavigationLink be accessible (can be interacted with)?

I've tried encapsulating my view in NavigationView and NavigationStack, neither behaved differently.

The view, as shown below, only the NavigationLinks in the ScrollView are accessible to interact with. The "Edit" NavigationLink cannot be selected to tap on. If I comment out the ScrollView NavigationLinks, then the "Edit" NavigationLink becomes accessible and functions correctly.

I've also tried replacing LazyVGrid with VStack to no effect.

import SwiftUI

struct TestSources: Hashable {
    let id = UUID()
    let name: String
}

struct SourcesView: View {

    private var Sources = [TestSources(name: "Computer 1"), TestSources(name: "Computer 2")]
    
    var columns: [GridItem] {
      Array(repeating: .init(.adaptive(minimum: 200)), count: 2)
    }
    
    var body: some View {
        NavigationStack {
            VStack(alignment: .center) {
                // Header
                HStack(alignment: .center){
                    Label("Sources", systemImage: "externaldrive.connected.to.line.below")
                        .font(.headline)
                        .frame(maxWidth: .greatestFiniteMagnitude, alignment: .leading)
                        .padding(.all)
                    
                    NavigationLink(destination: TestEditView()) {
                        Text("Edit")
                    }
                }
                
                Divider()
                
                ScrollView(.vertical, showsIndicators: false) {
                    LazyVGrid(columns: columns, spacing: 10) {
                        ForEach(Sources.indices, id: \.self) { index in
                            NavigationLink(Sources[index].name ,value: Sources[index])
                        }.navigationDestination(for: TestSources.self) { source in
                            TestShareView(source: source)
                        }
                        
                        .accentColor(Color.black)
                        .padding(Edge.Set.vertical, 20)
                    }
                    .padding(.horizontal)
                }
            }.frame(
                minWidth: 0,
                maxWidth: .infinity,
                minHeight: 0,
                maxHeight: .infinity,
                alignment: .topLeading
            )
        }
    }
}

struct TestEditView: View {
    var body: some View {
        Text("Edit")
    }
}

struct TestShareView: View {
    let source : TestSources
    
    var body: some View {
        Text(source.name)
    }
}

Upvotes: 0

Views: 175

Answers (1)

Alex Luque
Alex Luque

Reputation: 172

I don't see any problem with the navigation links in this code.

I pasted the code into a new project and tweaked it a little to make it compile. As you can see, it just works.

My guess is that it might fail because something outside of this code. Maybe, it is within another NavigationStack or some structure that could increse it's navigation complexity? Or as Yrb suggests, this force unwrapping could be failing because of null values?

enter image description here

Upvotes: 1

Related Questions