映射内部 VStack 导致崩溃

时间:2021-05-02 15:03:05

标签: swiftui

我有一个简单的 Map 视图:

struct MapContainer: View {
  @EnvironmentObject var store: AppStore
  @State private var region: MKCoordinateRegion = Location.defaultRegion
  var events: [Event] {
    store.state.getEventsFromSearchResults()
  }
  
  
  func select(id: UUID) {}
  
  
  var body: some View {
    Map(
      coordinateRegion: $region,
      interactionModes: .all,
      showsUserLocation: true,
      annotationItems: events,
      annotationContent: { event in
        MapAnnotation(coordinate: event.coordinates) {
          Image("MapMarker")
            .resizable()
            .scaledToFit()
            .frame(width: 24)
            .foregroundColor(
              event.id == store.state.searchState.tapped
                ? Color("Accent")
                : .black
            )
            .onTapGesture { select(id: event.id) }
        }
      }
    )
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .onAppear { region = store.state.searchState.near.region }
  }
}

我有一个使用上述容器的屏幕:

struct MapScreen: View {
  let height: CGFloat? = nil

  
  var body: some View {
    GeometryReader { geo2 in
      ZStack(alignment: .top) {
        VStack {
          MapContainer()
            .animation(.easeOut)
        }
        .frame(height: height != nil ? height : geo2.size.height / 2 + geo2.safeAreaInsets.top)
        

        VStack(spacing: 0) {
          VStack(spacing: 0) {
            HStack {
              Image(systemName: "minus")
                .font(.system(size: 40))
                .foregroundColor(Color("LightGray"))
                .padding(.vertical, 10)
            }
            .frame(maxWidth: .infinity)
            .contentShape(Rectangle())

            MapList()
          }
          .frame(height: 500)
          .background(Color.white.shadow(color: .gray, radius: 5, x: 0, y: 3))
        }
        .frame(maxHeight: .infinity, alignment: .bottom)
        .animation(.easeOut)


      }
      .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)
    .edgesIgnoringSafeArea(.vertical)

    }
  }
}

以上崩溃 - Thread 1: EXC_BAD_ACCESS (code=1, address=0x38) - 但如果我为 MapContainer 分配一个硬编码值,例如

MapContainer().frame(height: 400) 

然后一切正常。有谁知道是什么导致了这个问题?

0 个答案:

没有答案