1

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

6
  • Could you share a complete example of code to reproduce? Commented Nov 18, 2019 at 13:00
  • 1
    github.com/leontedev/Locally - sure, here is the project Commented Nov 18, 2019 at 13:09
  • 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.1 Commented Nov 18, 2019 at 13:16
  • 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). Commented Nov 18, 2019 at 13:18
  • 1
    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. Commented Nov 18, 2019 at 13:20

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.