ios 如何在SwiftUI中删除TextField / View的多余间距

ve7v8dk2  于 2023-02-14  发布在  iOS
关注(0)|答案(1)|浏览(219)

在下面的代码中,没有添加额外的填充。但是在预览或模拟器中,TextField和Map视图中有额外的填充/间距。是否有任何修改器可以删除额外的间距

import SwiftUI
import CoreLocation
import MapKit

struct LocationView: View {
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))

@State private var addressSearchString:String = ""

var body: some View {
    VStack {
        Text("Confirm Your Location").foregroundColor(.black).background(.blue)
        TextField("Search for area, street name...", text: $addressSearchString)
            .background(.blue.opacity(0.3))

        ZStack(alignment:.bottom) {
            Map(coordinateRegion: $region, interactionModes: [.all])
            Button(action: {}) {
                HStack {
                    Text("Locate Me")
                }
            }
            .foregroundColor(.green)
            .background(.white)
        }
        Text("Test 1")
            .background(.blue)
        Text("Test 2")
            .background(.green)
        Text("Test 3")
            .background(.blue)
        Text("Test 4")
            .background(.green)
        Text("Test 5")
            .background(.blue)

    }.background(.gray)

 }
}

ajsxfq5m

ajsxfq5m1#

删除相邻子视图之间的距离。

VStack(spacing: 0) { /* NEW */
    Text("Confirm Your Location").foregroundColor(.black).background(.blue)
    TextField("Search for area, street name...", text: $addressSearchString)
        .background(.blue.opacity(0.3))

    ZStack(alignment:.bottom) {
        Map(coordinateRegion: $region, interactionModes: [.all])
        Button(action: {}) {
            HStack {
                Text("Locate Me")
            }
        }
        .foregroundColor(.green)
        .background(.white)
    }
    Text("Test 1")
        .background(.blue)
    Text("Test 2")
        .background(.green)
    Text("Test 3")
        .background(.blue)
    Text("Test 4")
        .background(.green)
    Text("Test 5")
        .background(.blue)

}.background(.gray)

相关问题