swift - MapKit 错误 : NSHostingView is being laid out reentrantly while rendering its SwiftUI content

标签 swift macos cocoa swiftui mapkit

我已经使用 MapKit 实现了 Apple SwiftUI 教程的一个稍微改变的版本。 .
当我运行它时,我收到错误消息并调整我得到的相应 View 的大小:[SwiftUI] NSHostingView is being laid out reentrantly while rendering its SwiftUI content. This is not supported and the current layout pass will be skipped.查看调用代码:

    MapView(coordinate: location?.coordinates)
        .frame(minHeight: 200)
        .overlay(
            GeometryReader{
                proxy in
                Button("Open in Maps") {
                    if (self.location?.coordinates != nil){
                        let destination = MKMapItem(placemark: MKPlacemark(coordinate: self.location!.coordinates))
                        destination.name = "the car"
                        destination.openInMaps()
                    }
                }
                .frame(width: proxy.size.width, height: proxy.size.height, alignment: .bottomTrailing)
                .offset(x: -10, y: -10)
            }
    )
map View 结构:
import SwiftUI
import MapKit

/// a view containing the map
struct MapView: NSViewRepresentable {
    
    /// the location coordinates
    var coordinate: CLLocationCoordinate2D?
    
    func makeNSView(context: Context) -> MKMapView {
        MKMapView(frame: .zero)
    }
    
    func updateNSView(_ nsView: MKMapView, context: Context) {
        let span = MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)
        var region: MKCoordinateRegion = MKCoordinateRegion()
        if (self.coordinate != nil){
            region = MKCoordinateRegion(center: coordinate!, span: span)
        }
        nsView.setRegion(region, animated: true)
        if ((nsView.annotations.count == 0) && (self.coordinate != nil)){
            let locationPin: MKPointAnnotation = MKPointAnnotation()
            locationPin.coordinate = coordinate!
            nsView.addAnnotations([locationPin])
        }
    }
}

最佳答案

得到相同的消息并拼命尝试在 main.sync 中进行第一次调度,但在运行时出现异常。
然后我只是尝试了异步。

   {
      let region = MKCoordinateRegion(center: visibleArea.coordinate,
                                                latitudinalMeters: 200, longitudinalMeters: 200)
      DispatchQueue.main.async
      {
         nsView.setRegion(region, animated: false)
      }
   }
也适用于动画
      DispatchQueue.main.async
      {
         nsView.setRegion(region, animated: true)
      }
实际上,这暂时奏效了:它删除了
  • Xcode (12.3) 中的紫色信息消息
  • 删除控制台消息

  • 希望这只是一个小补丁,直到 Apple 清理它(或解释原因)。

    关于swift - MapKit 错误 : NSHostingView is being laid out reentrantly while rendering its SwiftUI content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63646954/

    相关文章:

    swift - 无法从解析表中检索数据

    macos - 在 osx 上的 Bamboo "Not privileged to remove service."上运行作业

    php - 请在启用 ZTS 的 Mac OS X 上重新编译 PHP

    macos - 可以在Cocoa中用Photoshop打开NSImage吗

    ios - SKNodes 之间的计算距离不会改变,尽管它们在移动

    swift - Xcode - Swift 计算属性的奇怪代码覆盖率

    ios - swift 中 nil 和空字符串之间基于性能的差异

    objective-c - 关于区 block 和中央调度的问题

    objective-c - 如何隐藏/关闭 NSWindowController 及其 View ?

    cocoa - Finder/Preview 显示的 NSImage 代码大小不同