【问题标题】:How to remove extra spacing of TextField / View in SwiftUI如何在 SwiftUI 中删除 TextField / View 的额外间距
【发布时间】:2023-02-13 22:23:57
【问题描述】:

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

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)

 }
}

【问题讨论】:

    标签: ios swift swiftui padding xcode14


    【解决方案1】:

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

      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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 2011-11-29
      • 2013-01-28
      • 2011-07-27
      • 2021-12-30
      • 1970-01-01
      相关资源
      最近更新 更多