簡體   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