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.
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
Button
over it withZStack
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 viewUIViewRepresentable
. Aha... Xcode 11.2.1 – Asperi