SwiftUI Buttons and their tappable areas shifted away and doesn't cover anymore

583 views Asked by At

I have the problems with views and especially buttons as they can be tapped (but it applies to all views) that they tappable (focus area) shifted a bit from rendered views. Layout stays ok but if you want to tap at the button its tappable area is shifted downward and to the right. So instead of clicking at the button you click a bit downa and right to it. Otherwise there is no action executed.

The best it can be seen on this view from Debug view hierarchy. It happend after returning from modal presentation. But I cannot see any clue in code way that can happen. We on which it happens seems to be no different from other views that work absolutly right.

I can see that wrapping VStack inside ScrollView breaks this view, but it doesn't happan on other similar views.

 var body: some View {
        NavigationView {
            ScrollView(.vertical, showsIndicators: false) {
                Group {
                    contentView
                    contentView
                    contentView
                    contentView
                    contentView
                    contentView
                    contentView
                    contentView
                }
                contentView
                contentView
                contentView
                contentView
                    
            }
            .padding(.horizontal)
        }
        .navigationViewStyle(.stack)
  
    }
    

enter image description here

1

There are 1 answers

0
Michał Ziobro On

Ok I found it. The problem was that content of this view depends on @Published property like shoulDisplayView1Or2. And depending whether it was true or false it displayed view1 or view2. Moreover this property was set in the same time when presented modal view (sheet) was dismissing. Like after some action do to things 1. dismiss modal and set new value of shouldDisplayView1Or2. And this sheet dismissal and adjuting view1 or view2 based on shouldDisplayView1Or2 flag caused that this buttons rendered areas and their tappable areas become shifted away.

What fixed this issue was addition of DispatchQueue.main in Combine subscription or DispatchQueue.main.async { } in regular code in place where this flag shouldDisplayView1Or2 was setting. This way I suppose dismissing comes first and adjusting view1 or views with its rendering second, and overall layout wast rendered correctly with tappable areas on its place.