Search code examples
swiftuiswiftui-navigationlink

NavigationLink touch area


I'm trying to use NavigationLink inside List. For a specific reason, it is in .background with EmptyView().

var body: some View {
    List {
        Section {
            HStack {
                Image(systemName: "checkmark")
                    .frame(width: 30, height: 30, alignment: .center)
                    .opacity(selected ? 1 : 0)
                Text("TEST")
                Spacer()
            }
            .onTapGesture {
                selected.toggle()
            }
            .background(
                NavigationLink(destination: WifiView()) { EmptyView() }
                    .disabled(isPad)
            )
        }
    }
}

The problem is the touch area for the NavigationLink is not as expected.

.background area

.background's area is as above.

NavigationLink and EmptyView area

but NavigationLink and EmptyView's area is as above. I tried to force the frame with .frame but it won't change.

What am I missing?


Solution

  • try something like this, works well for me:

    var body: some View {
        List {
            Section {
                HStack {
                    Image(systemName: "checkmark")
                        .frame(width: 30, height: 30, alignment: .center)
                        .opacity(selected ? 1 : 0)
                    Text("TEST")
                    Spacer()
                }
                    .contentShape(Rectangle())  // <--- here
                    .onTapGesture {
                        selected.toggle()
                    }
                    .background(
                        NavigationLink(destination: WifiView()) { EmptyView() }
                            .disabled(isPad)
                    )
            }
        }
    }