Posts

Post not yet marked as solved
0 Replies
402 Views
I have a NSViewRepresentable over a custom NSTextField, and I can't seem to be able to detect clicks on it if it's part of a List. Neither NSClickGestureRecognizer, nor onTapGesture work. This is a minimal example exhibiting the problem: struct MainView: View { let items = ["One", "Two", "Three"] var body: some View { VStack { List(items, id: \.self) { item in TextFieldRepresentable(item: item) .onTapGesture { print("onTapGesture") } } } } } struct TextFieldRepresentable: NSViewRepresentable { let item: String func makeNSView(context: Context) -> some NSView { let label = NSTextField(labelWithString: item) let clickRecognizer = NSClickGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.onClick)) label.addGestureRecognizer(clickRecognizer) return label } func makeCoordinator() -> Coordinator { Coordinator() } func updateNSView(_ nsView: NSViewType, context: Context) { } class Coordinator { @objc func onClick() { print("onClick") } } } Am I missing something here? P.S. If I switch from List to ForEach, the NSClickGestureRecognizer works, but onTapGesture not (I assume onTapGesture works on "pure" SwiftUI views), however since I'm able to detect clicks, that's really OK, and works better since NSClickGestureRecognizer is the one I need to work.
Posted
by kelucho.
Last updated
.