首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将按钮/视图放在SwiftUI地图的顶部?

如何将按钮/视图放在SwiftUI地图的顶部?
EN

Stack Overflow用户
提问于 2020-08-02 10:47:55
回答 2查看 398关注 0票数 1

我找不到一种方法将我的buttonView (只是一个按钮)放在地图的顶部,这样我就可以点击它。在另一个更复杂的设置中,按钮以某种方式出现在顶部,我可以点击它,但这是幸运的,而不是故意的。如何让我的buttonView出现在地图上,这样我就可以点击它了?

注意,我认为问题可能是我的buttonView“在”某个地图图层下,因此地图捕获了点击事件,而不是将它们传递给我的buttonView。

Xcode 12 beta-3,mac catalina,目标ios 14。

代码语言:javascript
复制
import Foundation
import SwiftUI
import MapKit
import CoreLocation
@main
 struct TestMapApp: App {
    var body: some Scene {
            WindowGroup {
                    MapViewer()
            }
    }
}
struct MapViewer: View {
    @State var cityAnno = [CityMapLocation(title: "Tokyo", subtitle: "Japan", lat: 35.685, lon: 139.7514)]
    @State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 35.685, longitude: 139.7514),span: MKCoordinateSpan(latitudeDelta: 1.0, longitudeDelta: 1.0))
    
    var body: some View {
            Map(coordinateRegion: $region, annotationItems: cityAnno) { city in
                    MapAnnotation(coordinate: city.coordinate) {
                            buttonView(cityName: city.title!)
                            // tried this, does not work
                            // Image(systemName:"dot.circle.and.cursorarrow").foregroundColor(.white).scaleEffect(2.2)
                            //   .onTapGesture { print("----> onTapGesture") }
                    }
            }
    }
    func buttonView(cityName: String) -> some View {
            Button(action: {print("----> buttonView action")}) {
                    VStack {
                            Text(cityName)
                            Image(systemName: "dot.circle.and.cursorarrow")
                    }.foregroundColor(.red).scaleEffect(1.2)
            }.frame(width: 111, height: 111)
            // tried combinations of these, without success
//              .background(Color.gray).opacity(0.8)
//              .border(Color.white)
//              .contentShape(Rectangle())
//              .clipShape(Rectangle())
//              .zIndex(1)
//              .buttonStyle(PlainButtonStyle())
//              .layoutPriority(1)
//              .allowsHitTesting(true)
//              .onTapGesture {
//                      print("----> onTapGesture")
//              }
    }
    }
    class CityMapLocation: NSObject, MKAnnotation, Identifiable {
    var id = UUID().uuidString
    var title: String?
    var subtitle: String?
    dynamic var coordinate: CLLocationCoordinate2D
    
     init(title: String?, subtitle: String?, lat: Double, lon: Double) {
            self.id = UUID().uuidString
            self.title = title
            self.subtitle = subtitle
            self.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
    }
    }
EN

回答 2

Stack Overflow用户

发布于 2021-02-09 17:32:59

只需将它们包装在ZStack中:

代码语言:javascript
复制
ZStack {
    Map(coordinateRegion: $region, annotationItems: cityAnno){...}
    Button(action: {print("----> buttonView action")}) {...}
}
票数 0
EN

Stack Overflow用户

发布于 2021-02-10 14:30:01

你可以只在Vstack上获得onTapGesture。通过替换MapViewer的主体来尝试下面的代码。

代码语言:javascript
复制
var body: some View {
    Map(coordinateRegion: $region, annotationItems: cityAnno) { city in
        MapAnnotation(coordinate: city.coordinate) {
            VStack {
                Text(city.title ?? "")
                Image(systemName: "dot.circle.and.cursorarrow")
            }
            .foregroundColor(.red).scaleEffect(1.2)
            .frame(width: 111, height: 111)
            .onTapGesture {
                print("Clicked")
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63211898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档