Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigationLink only works on long press SwiftUI

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.

like image 381
Hayden Hong Avatar asked Sep 05 '25 03:09

Hayden Hong


2 Answers

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()
}
like image 184
Hayden Hong Avatar answered Sep 07 '25 19:09

Hayden Hong


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
like image 31
Pratheesh Bennet Avatar answered Sep 07 '25 19:09

Pratheesh Bennet