4

For some reason it seems that my NavigationLink is only working on long press. Here's a snippet of the View:

struct MainView: View {
    @EnvironmentObject var user: UserObservable
    var body: some View {
        VStack {
            NavigationView {
                List(user.items, id: \.self) { item in
                    NavigationLink(destination: ItemView(item: item)) {
                        Text(item.name)
                    }
                }
                .navigationBarTitle("\(user.displayName)'s items")
                .navigationBarItems(leading: AddItemViewButton().environmentObject(user),
                                    trailing: MainViewActionSheet().environmentObject(user))
            }
        }
    }
}

The list is populated correctly but tapping on them does nothing. Pressing and holding and then releasing does open the correct destination.

Has anyone else seen anything like this? This is on Xcode 11.4.1 and iOS 13.4.1.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Hayden Hong
  • 135
  • 1
  • 9
  • 1
    do you get the same behaviour if you put the NavigationView before the VStack? – workingdog support Ukraine Apr 17 '20 at 02:12
  • If I put the VStack inside the NavigationView or just remove the VStack wrapper altogether (I kinda forgot it was there) the bug remains. This same code worked for me on previous versions of Xcode too, which is strange. – Hayden Hong Apr 17 '20 at 02:50
  • 1
    Your code looks correct. Is behaviour the same if to comment `navigationBarItems`? If yes then it seems updated Xcode defect. I have not updated yet. – Asperi Apr 17 '20 at 04:11

2 Answers2

7

Thanks to all who helped, it turns out my issue was I had left an onTapGesture in my SceneDelegate that was supposed to close the keyboard when typing but instead kept my NavigationLinks from opening. Oops.

It looked a bit like this in case anyone runs into this issue too:

// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
    let window = UIWindow(windowScene: windowScene)
    window.rootViewController = UIHostingController(rootView: ContentView()
        .environmentObject(user)
        .onTapGesture { window.endEditing(true) }) // <- Problem right there
    self.window = window
    window.makeKeyAndVisible()
}
Hayden Hong
  • 135
  • 1
  • 9
2

In my case also, my custom view had an onTapGesture. On removing the gesture, the issue got resolved.

  CarouselContentView(data: eachItem)
        .frame( height: 200, alignment: .center).onTapGesture {   
        } <== Removed it