UIViewRepresentable:
import SwiftUI
import MapKit
import CoreData
struct CustomView: UIViewRepresentable {
var coordinate: CLLocationCoordinate2D
func makeUIView(context: Context) -> MKMapView {
MKMapView(frame: .zero)
}
func updateUIView(_ uiView: MKMapView, context: Context) {
let span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
let region = MKCoordinateRegion(center: coordinate, span: span)
uiView.setRegion(region, animated: true)
}
}
ContentView:
import SwiftUI
import CoreLocation
struct ContentView: View {
var body: some View {
GeometryReader { geo in
CustomView(coordinate: CLLocationCoordinate2D(latitude: 37.33182, longitude: -122.03118))
.frame(width: geo.size.width, height: 300)
.cornerRadius(25)
.contextMenu(/*@START_MENU_TOKEN@*/ContextMenu(menuItems: {
Text("Menu Item 1")
Text("Menu Item 2")
Text("Menu Item 3")
})/*@END_MENU_TOKEN@*/)
}
.padding(.horizontal)
.frame(height: 300)
}
}
The above code works fine in the SwiftUI Canvas and the Simulator, however on my physical testing device (an iPhone 7 - iOS 14 Beta 5), when I long press the CustomView, it becomes black. The app also sometimes crashes with the following error which may be related:
CGImageCreate: invalid image alphaInfo: kCGImageAlphaNone. It should be kCGImageAlphaNoneSkipLast
If I replace the CustomView with an Image like below, everything works as expected:
import SwiftUI
struct ContentView: View {
var body: some View {
Image("imageName")
.resizable()
.frame(width: 350, height: 300)
.cornerRadius(25)
.contextMenu(/*@START_MENU_TOKEN@*/ContextMenu(menuItems: {
Text("Menu Item 1")
Text("Menu Item 2")
Text("Menu Item 3")
})/*@END_MENU_TOKEN@*/)
}
}
How can I fix it? Thanks!