我正在尝试将我的Picker隐藏标签值与表单中的另一个TextField (see attached image)对齐。在显示的选择器值中删除8px填充的最好方法是什么?
import SwiftUI
struct ContactFormView: View {
var countries = ["Malaysia", "Singapore", "Japan"]
@State var name: String = ""
@State var mobile: String = ""
@State var mobileCountry: String = "Malaysia"
var body: some View {
NavigationView {
Form {
Section(header: Text("Details")) {
TextField("Name", text: $name)
.border(Color.red, width: 1)
HStack {
Picker(selection: $mobileCountry, label: EmptyView()) {
ForEach(countries, id: \.self) {
Text($0).border(Color.red)
}
}
.scaledToFit()
.labelsHidden()
.border(Color.red, width: 1)
TextField("Mobile", text: $mobile)
.border(Color.red, width: 1)
}
}
}
.navigationBarTitle("New contact")
}
}
}

发布于 2021-05-06 13:55:39
也许你最好的办法就是在你的选择器上使用负数填充:
Picker(selection: $mobileCountry, label: EmptyView()) {
ForEach(countries, id: \.self) {
Text($0).border(Color.red)
}
}
.scaledToFit()
.labelsHidden()
.border(Color.red, width: 1)
.padding(.horizontal, -8)在Xcode12.5中加载代码,添加负边距可以按您想要的方式对齐文本。
直观地说,我会选择.padding(.leading, -8)只删除选取器前缘的填充-但这样做,选取器和文本字段之间的差距就会变大。
将负填充应用于两个水平值,使该间距对我来说是相同的。但我建议您在代码中同时尝试这两种方法,看看哪一种对您最有意义。
https://stackoverflow.com/questions/67412296
复制相似问题