Home > Software design >  When SwiftUI View is set to the content of MapAnnotation, the frame does not update properly when Vi
When SwiftUI View is set to the content of MapAnnotation, the frame does not update properly when Vi

Time:08-24

In the body, I implemented each View in the if else syntax . Every time I click the button to toggle the boolean value and check the UI, it works perfectly as I think.

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()
            }
        }
    }
}

enter image description here

However, the UI looks strange when the same view is just in MapAnnotation. As before, after clicking on the button to toggle the boolean value and check the UI, the Text is truncated.

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
                
                //            
  • Related