I have an extremely simple set up in which a MapKit resides within another view. I want the map to be able to be expanded into a fullScreenCover
. The map properly shows up in ProfilePageView on initial load, and properly shows up in the fullScreenCover
when expanded. However, when the fullScreenCover
is exited the map is effectively totally gone (see image below). Clicking on the rectangle again properly sends it back to the fullScreenCover
where it is displaying correctly.
I can force it to rerender by using a UUID key, but this seems like a hack and I don’t understand why it is necessary.
import Foundation
import SwiftUI
import UIKit
import MapKit
struct ProfilePageView: View {
@State private var isMapExpanded: Bool = false
@State private var mapView: MKMapView
@State private var mapViewKey: UUID = UUID()
init(countryNames: [String]) {
// Initialize MKMapView with configuration
let initialMapView = MKMapView()
let initialLocation = CLLocationCoordinate2D(latitude: 33.9391, longitude: 67.7100)
let regionRadius: CLLocationDistance = 6000000
let coordinateRegion = MKCoordinateRegion(center: initialLocation, latitudinalMeters: regionRadius, longitudinalMeters: regionRadius)
initialMapView.setRegion(coordinateRegion, animated: true)
_mapView = State(initialValue: initialMapView)
print("MKMapView initialized in init")
}
var body: some View {
ScrollView {
Divider()
Button(action: {
isMapExpanded = true
}) {
ZStack {
MapUserView(isMapLoading: .constant(false), mapView: mapView)
.id(mapViewKey) // Use the key to force recreation
.frame(height: 425)
.cornerRadius(10)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.blue, lineWidth: 4)
)
.allowsHitTesting(false)
Rectangle()//
.foregroundColor(.clear)
.frame(height: 425)
.contentShape(Rectangle())
}
}
.buttonStyle(PlainButtonStyle())
.fullScreenCover(isPresented: $isMapExpanded, onDismiss: {
print("FullScreenCover dismissed")
mapViewKey = UUID() // *If this is commented it we get the above described image*
}) {
MapUserFullScreenView(isPresented: $isMapExpanded, mapView: mapView)
}
Text("Here we are")
}
}
}
struct MapUserFullScreenView: View {
@Binding var isPresented: Bool
var mapView: MKMapView
var body: some View {
ZStack(alignment: .topLeading) {
MapUserView(isMapLoading: .constant(false), mapView: mapView)
.edgesIgnoringSafeArea(.all)
Button(action: {
isPresented = false
}) {
Image(systemName: "xmark.circle.fill")
.padding()
.font(.largeTitle)
.foregroundColor(.white)
}
.edgesIgnoringSafeArea(.all)
}
}
}
struct ProfilePageView_Previews: PreviewProvider {
static var previews: some View {
let randomCountries = ["Brazil", "Colombia"]
return ProfilePageView(countryNames: randomCountries)
}
}
import SwiftUI
import MapKit
struct MapUserView: UIViewRepresentable {
@Binding var isMapLoading: Bool
var mapView: MKMapView
func makeUIView(context: Context) -> MKMapView {
print("UI has been made")
// Ensure the map view is only configured once
if mapView.delegate == nil {
mapView.delegate = context.coordinator
}
return mapView
}
func updateUIView(_ mapView: MKMapView, context: Context) {
print("updateUIView called")
// Ensure the map view is not reset or cleared unintentionally
if mapView.delegate == nil {
mapView.delegate = context.coordinator
}
}
func makeCoordinator() -> MapUserCoordinator {
MapUserCoordinator(self)
}
class MapUserCoordinator: NSObject, MKMapViewDelegate {
var parent: MapUserView
private var cancellables = Set<AnyCancellable>()
var dimmingView: UIView?
init(_ parent: MapUserView) {
self.parent = parent
super.init()
}
func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
parent.isMapLoading = false
}
}
}
struct MapUserView_Previews: PreviewProvider {
static var previews: some View {
return MapUserView(isMapLoading: .constant(false), mapView: MKMapView())
}
}
Why is this UUID key necessary? Why isn’t @State
properly ensuring the state of the mapview remains shared within the ProfilePageView
and that of the value displayed in the fullScreenCover
?
Note that I am passing mapView
to MapUserView
to enable the fullscreencover to display without requiring a full reload of the map information (I have stripped out a lot of the complex methodologies here to focus on the specific problem). Prior to passing this mapView
, the button would cause the map to entirely reload before displaying and this would cause significant lag.