如何解决带有UIViewRepresentable的SwiftUI上下文菜单的意外视觉行为
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)
}
}
上面的代码在SwiftUI Canvas和Simulator中可以正常工作,但是在我的物理测试设备(iPhone 7-iOS 14 Beta 5)上,当我长按CustomView时,它会变成黑色。该应用有时有时还会因以下可能的错误而崩溃:
CGImageCreate: invalid image alphaInfo: kCGImageAlphaNone. It should be kCGImageAlphaNoneSkipLast
如果我将CustomView替换为下面的图像,则一切正常:
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@*/)
}
}
我该如何解决?谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。