繁体   English   中英

当SwiftUI View设置为MapAnnotation的内容时,View改变时frame没有正确更新

[英]When SwiftUI View is set to the content of MapAnnotation, the frame does not update properly when View is changed

body中,我用if else语法实现了每个View 每次我单击按钮切换 boolean 值并检查 UI 时,它都像我想的那样完美运行。

struct ContentView: View {
    
    @State private var isSelected: Bool = false
    var body: some View {
        VStack {
            if isSelected {
                Text("isSelected" )
                    .font(.title)
                    .foregroundStyle(.blue)
                    .background(.green)
            } else {
                Text("isNotSelected")
                    .font(.callout)
                    .foregroundStyle(.blue)
                    .background(.red)
            }
            
            Button("Change select state") {
                isSelected.toggle()
            }
        }
    }
}

在此处输入图像描述

但是,当同一视图仅在MapAnnotation中时,UI 看起来很奇怪。 和以前一样,单击按钮切换 boolean 值并检查 UI 后, Text被截断。

struct Place: Identifiable {
    let id = UUID()
    let latitude: Double
    let longitude: Double
}

struct ContentView: View {
    
    @State private var isSelected: Bool = false
    @State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 37.566535,
                                                                          longitude: 126.9779692),
                                           span: MKCoordinateSpan(latitudeDelta: 0.5,
                                                                  longitudeDelta: 0.5))

    let places: [Place] = [
        Place(latitude: 37.566535,
              longitude: 126.9779692),
        Place(latitude:35.1795543,
              longitude: 129.0756416)
    ]
    
    var body: some View {
        VStack {
            Map(coordinateRegion: $region,
                annotationItems: places) { place in
                
                // 🙌🏻🙌🏻🙌🏻🙌🏻 used as MapAnnotation content
                MapAnnotation(coordinate: CLLocationCoordinate2D(latitude: place.latitude,
                                                                 longitude: place.longitude)) {
                    if isSelected {
                        Text("isSelected" )
                            .font(.title)
                            .foregroundStyle(.blue)
                            .background(.green)
                    } else {
                        Text("isNotSelected")
                            .font(.callout)
                            .foregroundStyle(.blue)
                            .background(.red)
                    }
                } // MapAnnotation
            }
            
            Button("Change select state") {
                isSelected.toggle()
            }
        }
    }
}

在此处输入图像描述

即使我不使用if else语法来分隔View ,并使用带有三元运算符的单个Text View 设置值,UI 看起来还是像以前一样奇怪。

MapAnnotation(coordinate: CLLocationCoordinate2D(latitude: place.latitude,
                                                 longitude: place.longitude)) {
    Text(isSelected ? "isSelected" : "isNotSelected")
        .font(isSelected ? .title : .callout)
        .foregroundStyle(.blue)
        .background(isSelected ? .green : .red)
}

当我滚动 map 时,UI 会按预期更新。

在此处输入图像描述

这是 SwiftUI 错误吗? 还是我错过了什么?

给它固定大小,例如

MapAnnotation(coordinate: CLLocationCoordinate2D(latitude: place.latitude, longitude: place.longitude)) {
    VStack {
        if isSelected {
            Text("isSelected" )
                .font(.title)
                .foregroundStyle(.blue)
                .background(.green)
        } else {
            Text("isNotSelected")
                .font(.callout)
                .foregroundStyle(.blue)
                .background(.red)
        }
    }
    .fixedSize()     // << here !!
} // MapAnnotation

实际上我们不知道内部 Map 对注释大小的期望,所以我不能说这是一个错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM