swiftui - 如何在 SwiftUI 的新 map View 中制作可点击的图钉?

标签 swiftui ios14

我正在使用 SwiftUI 的新 map View 来显示注释的大头针,并希望大头针在单击时显示用于编辑注释名称和描述的 View 。
我尝试将 MapAnnotation 与包含带有图钉图像的按钮的 View 一起使用。图像显示正确,但按钮不起作用。
是否可以在不回退到 MKMapView 的 UIViewRepresentable 的情况下执行此操作?

import SwiftUI
import MapKit

struct ContentView: View {
    @State private var showingEditScreen = false

    @State private var region = MKCoordinateRegion(
        center: CLLocationCoordinate2D(latitude: 37.334722,
                                       longitude: -122.008889),
        span: MKCoordinateSpan(latitudeDelta: 1,
                               longitudeDelta: 1)
    )

    @State private var pins: [Pin] = [
        Pin(name: "Apple Park",
            description: "Apple Inc. headquarters",
            coordinate: CLLocationCoordinate2D(latitude: 37.334722,
                                               longitude:-122.008889))
    ]

    var body: some View {
        Map(coordinateRegion: $region,
            interactionModes: .all,
            annotationItems: pins,
            annotationContent: { pin in
                MapAnnotation(coordinate: pin.coordinate,
                              content: {
                                PinButtonView(pin: pin)
                              })
            })
    }
}
我的引脚定义:
struct Pin: Identifiable {
    let id = UUID()
    var name: String
    var description: String
    var coordinate: CLLocationCoordinate2D

}
带有按钮和图钉图像的 View :
struct PinButtonView: View {
    @State private var showingEditScreen = false
    @State var pin: Pin

    var body: some View {
        Button(action: {
            showingEditScreen.toggle()
        }) {
            Image(systemName: "mappin")
                .padding()
                .foregroundColor(.red)
                .font(.title)
        }
        .sheet(isPresented: $showingEditScreen,
               content: {
                EditView(pin: self.$pin)
               })
    }
}
编辑 View :
struct EditView: View {
    @Environment(\.presentationMode) var presentationMode

    @Binding var pin: Pin

    var body: some View {
        NavigationView {
            Form {
                    TextField("Place name", text: $pin.name)

                    TextField("Description", text: $pin.description)
            }
            .navigationTitle("Edit place")
            .navigationBarItems(trailing: Button("Done") {
                self.presentationMode.wrappedValue.dismiss()
            })
        }
    }
}

最佳答案

从 XCode 12.3 开始,这似乎可以与按钮一起使用。我注意到的一个关键问题是,如果您使用偏移量,那么您的按钮可能会被放置在注释的可点击区域之外。
为了解决这个问题,您可以添加额外的填充来解释偏移并将其保持在可点击的范围内:

MapAnnotation(coordinate: item.placemark.coordinate) {
  ZStack {
      MapPinView() // My view for showing a precise pin
      VStack { // A prompt with interactive option, offset by 60pt
          Text("Use this location?")
          HStack {
              Button("Yes", action: {
                  print("yes")
              })
              
              Button("No", action: {
                  print("no")
              })
          }
      }
      .offset(x: 0, y: 60) // offset prompt so it's below the marker pin
  }
  .padding(.vertical, 60) // compensate for offset in tappable area of annotation. pad both the top AND the bottom to keep contents centered
}

关于swiftui - 如何在 SwiftUI 的新 map View 中制作可点击的图钉?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63211092/

相关文章:

swift - 如何在 SwiftUI 中设置 addObserver?

xcode - SwiftUI:自动预览更新始终暂停

SwiftUI 嵌套 ForEach 导致 fatal error : each layout item may only occur once

iOS 14 Beta - Swift UI 中的核心数据错误 : No NSEntityDescriptions in any model claim the NSManagedObject subclass

xcode11 - iOS 14 启动屏幕 Storyboard 不会在 iOS 14 设备上显示图像

过滤 ForEach 时,SwiftUI 导航链接不起作用

if-statement - SwiftUI ForEach 不能在内部使用 if block 进行编译 - 错误还是我做错了什么?

swift - 在 Xcode11 beta 4 中工作但在 beta 5 中停止工作的代码有问题

swift - iOS 14 SwiftUI 键盘自动提升 View

与从 IDE 构建/运行相比,iOS 14 不允许 Flutter 应用程序(仍处于开发阶段)从主屏幕启动