UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Why does using LazyHStack with ScrollView break .onTapGesture/.sheet in a child view?

Forums > SwiftUI

I have encountered a learning opportunity where I could use some input. I was having a problem with initiating a sheet view based on a tapGesture in a child view of a ScrollView.

Everything worked as expected up to the .onTapGesture in PhotoPickerImageView. When tapping on the thumbnail that is presented, the sheet (ImageViewer) would not display. As soon as I changed the LazyHStack in ItemsFilteredView to an HStack, the problem was resolved. That is, tapping on the thumbnail in PhotoPickerView initiates the ImageViewer sheet view. Why am I not able to use the LazyHStack in this scenario?

I have fixed the problem, but would like to understand why. thanks -- jay

struct ItemsFilteredView: View {
    ...
    ScrollView(.horizontal) {
        LazyHStack {   // after changing this to HStack, the problem resolved
            ForEach(items) { item in
                ItemView(item: item)
    ...
struct ItemView: View {
    ...
    NavigationStack {
        VStack {
            ...
            LazyVGrid(columns: columns) {
                ForEach(data, id: \.self) { data in
                    let uiImage = UIImage(data: data)
                    PhotoPickerImageView(uiImage: uiImage!)
            ...
struct PhotoPickerImageView: View {
    ...
    Image(uiImage: uiImage)
        .onTapGesture {
            isImageViewerPresented = true
        }
        .sheet(isPresented: $isImageViewerPresented) {
            ImageViewer(

2      

@shan0011 thanks for taking the time to respond. I'm wondering if you were answering a question from a different post. My problem had nothing to do with the error or the code you described. See my description above for my scenario.

2      

anyone have feedback on this? I would like to understand the issue. And, ideally, I would like to use the LazyHStack if I could get it to work as expected. thanks

2      

One last bounce... hoping for some guidance / education on this one. I would like to return to LazyHStack, but can't for the reasons I describe above.

   

Hi @jayelevy

  1. Do you have a NavigationStack in the Parent View (ItemsFilteredView)? If so remove it from ItemView

  2. Have you tried putting it in a Button?

    Button {
    isImageViewerPresented = true
    } label: {
    Image(uiImage: uiImage)
    }
    .buttonStyle(.plain)

1      

@NigelGee thanks for the response. No, no NavigationStack in the (Grand)parent view (ItemsFilteredView). Unfortunately the Button approach makes no difference.

As I've continued to troubleshoot, I am observing the following behavior.

The tap (or button press) is successful (verified through a print statement immediately prior to setting isImageViewerPresented). As I scroll through the LazyHStack and tap on photos it seems the view I expect (ImageViewer) is put on a stack somewhere.

Meaning... after I tap on a few photos in the view within the LazyHStack, the ImageViewer view does not display as expected. HOWEVER, if I then change the filtered view in the grandparent (ItemsFilteredView), the images start appearing via ImageViewer -- sequentially, one after another for each photo that was tapped, as if they were queued views.

This behavior does not happen if I use the HStack instead of LazyHStack.

I imagine that is confusing to follow.

Troubleshooting this queued (but hidden!) stack of views is outside of my current skill level.

edit: I realized, in fact, I do have a NavigationStack at the topmost view. I removed the NavigationStack in ItemView as it was serving no purpose. Still no difference, though, in the behavior I describe above. Still not working with LazyHStack

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.