Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Buttons in a ScrollView is not tappable sometimes

Tags:

swiftui

I have a horizontal ScrollView on top of a MapView.

The ScorllView is a collection of Buttons. It is weird that the buttons in the ScrollView are sometime tapable and sometimes not. First tap always works but after that I have to scroll a bit, tap around different areas in the button, make some secret prayers and then it works!

I tried disabling/removing all other components in the view, but still unable to figure out the root cause.

Has anyone experience this ?

like image 360
TiN Avatar asked Feb 01 '26 16:02

TiN


2 Answers

I stuck with a same issue with horizontal ScrollView on top and List. While debugging I added empty .onTapGesture to ScrollView and it somehow fix my issue.

VStack(spacing: 0) {
    ScrollView(.horizontal) {
        HStack {
            Button("one") {}
            Button("two") {}
            Button("three") {}
        }
    }
    .onTapGesture { // <---- fix
    }
    List {
    }
}
like image 197
Evgeniy Vaganov Avatar answered Feb 03 '26 06:02

Evgeniy Vaganov


This is the default behavior of UIScrollView. You can disable it by running this code somewhere like inside init or .onAppear modifier:

UIScrollView.appearance().delaysContentTouches = false

⚠️ Note that this will be applied on all scrollViews. You may want to make it true again if you like the default behavior back.

like image 41
Mojtaba Hosseini Avatar answered Feb 03 '26 05:02

Mojtaba Hosseini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!