【问题标题】:SwiftUI Button on top of a MKMapView does not get triggered不会触发 MKMapView 顶部的 SwiftUI 按钮
【发布时间】:2020-12-29 13:29:06
【问题描述】:

我在 MKMapView 顶部有一个按钮。但是当它被点击时按钮不会被触发。你知道缺少什么吗?

MapView.swift

import SwiftUI
import UIKit
import MapKit

struct MapView: UIViewRepresentable {

    func makeUIView(context: Context) -> MKMapView {
        let mkMapView = MKMapView()
        return mkMapView
    }
    
    func updateUIView(_ uiView: MKMapView, context: Context) { }
    
    func makeCoordinator() -> Coordinator {
        Coordinator()
    }
    class Coordinator: NSObject, MKMapViewDelegate { }
}

ContentView.swift

import SwiftUI

struct ContentView: View {
    var body: some View {
        ZStack {
            MapView()
            Button(action: {
                print("Tapped")
            }) {
                Image(systemName: "lock.fill")
            }
        }
    }
}

【问题讨论】:

    标签: swift io swiftui mkmapview zstack


    【解决方案1】:

    给它更多的内部空间,以便更好地识别。这是经过修复和测试的变体(Xcode 12 / iOS 14):

    struct TestButtonWithMap: View {
        @State private var locked = true
        var body: some View {
            ZStack {
                MapView()
                Button(action: {
                    print("Tapped")
                    self.locked.toggle()
                }) {
                    Image(systemName: locked ? "lock.fill" : "lock.open")
                        .padding()      // << here !!
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多