1
votes

I have a SwiftUI project. I want to have a MKMapView with some SwiftUI buttons laid out on top.

So I've defined a MapView struct which conforms to the UIViewRepresentable protocol. And in my ContentView I have the following:

ZStack {
  MapView() {}

  Button() {}
}

My expectation is to have the Button placed on top of the MapView, however the behaviour is very weird. It indeed draws the SwiftUI button on top, but a tap gesture for example, does not stop on the SwiftUI button as I would expect. It passes through to the MapView.

The Debug View Hierarchy shows the MapView on top of the SwiftUI Button. enter image description here

I've tried a bunch of things: setting the .zIndex manually on all the views. LayoutPriority. Overlay instead of the ZStack. But the MapView is always on top. I can't get the behavior I want no matter what I try... Could this be a SwiftUI bug, or am I missing something?

The project can be found here: https://github.com/leontedev/Locally

1
Could you share a complete example of code to reproduce?krjw
github.com/leontedev/Locally - sure, here is the projectMihaiL
I've downloaded Apple's tutorial with MapView Creating and Combining Views and tried to add Button over it with ZStack as you described. As a result all works as expected: button is drawn over map view and captures tap gesture. So, probably the issue in your code of map view UIViewRepresentable. Aha... Xcode 11.2.1Asperi
That's not the issue I described. Indeed the button is drawn over the map & captures the tap gesture, but check the Debug View Hierarchy - is it placed underneath? If that's the case, this is going to cause other issues (as I described).MihaiL
If you see it everywhere on top and it works as expected, then why it is a problem? It might be Xcode's DebugView defect. ... And BTW, your question is not about Xcode, but sounds as it does not work at all, however as I noted "but a tap gesture for example, does not stop on the SwiftUI button" is not observed in Apple's example code.Asperi

1 Answers

1
votes

Ok.... I've identified the issue... it's the .shadow(radius: 6) modifier on the SwiftUI button which causes this issue. Which is very strange... but hey at least I've found the issue.