xcode - 未触发时,表单中的 SwiftUI 条件文本字段显示为空白
问题描述
我根据上一个问题制作了一个具有条件 TextFields 的表单(例如,你曾经旅行过吗?(布尔答案)下一个问题:如果是“在哪里?”(没有别的)。
当不满足显示下一个问题的条件时,我的表单中会出现一个空格(请参见屏幕截图)(如果满足条件,则一切正常)。
我不希望这些空白出现,因为它看起来不太好。我该如何解决这个问题?
这是我的代码:
var body: some View {
Form {
Section(header: Text("Work Order")) {
TextField("Work order title", text: $wovm.workorder.title)
Picker("Brand",selection: $wovm.workorder.brand) {
Group {
Text("Beneteau").tag(Brand.brand1)
Text("Jeanneau").tag(Brand.brand2)
Text("IslandP").tag(Brand.brand3)
}
}
}
Section(header: Text("Description of the boat")) {
VStack {
TextField("Description of the boat", text: $wovm.workorder.description)
}
HStack {
Text("Brand")
Spacer()
TextField("entrer date", text: $wovm.workorder.symptomesqd)
.multilineTextAlignment(.trailing)
}
Picker("Is it a pre-owned boat",selection: $wovm.workorder.preowned) {
Group {
Text("Pre-Owned").tag(Preowned.preowned)
Text("Not pre-owned").tag(Preowned.notpreowned)
Text("I don’t know").tag(Preowned.noidea)
}
}
HStack {
Picker("Is this the first repais of this type?", selection: $wovm.workorder.firstrepair){
Text("Oui").tag(true)
Text("Non").tag(false)
}
}
HStack {
if wovm.workorder.firstrepair == false {
}
else {
TextField"When was the last time it happened", text: $wovm.workorder.lasttime)
.multilineTextAlignment(.trailing)
}
}
}
HStack {
if wovm.workorder.firstrepair == false {
}
else {
TextField("Who made the previous work order ?", text: $wovm.workorder.whomade)
}
}
}
Section(header: Text("Facteurs extérieurs")) {
HStack {
Picker("Is this urgent", selection: $wovm.workorder.urgent){
Text("Oui").tag(Urgent.oui)
Text("Non").tag(Urgent.non)
Text("Je ne sais pas").tag(Urgent.saispas)
}
}
HStack {
Picker("Have you had similar problems in the past?", selection: $wovm.workorder.similarpb){
Text("Oui").tag(true)
Text("Non").tag(false)
}
}
HStack {
if wovm.workorder.similarpb == true {
Picker("Do you think they could be linked to this WO?", selection: $wovm.workorder.linked){
Text("Oui").tag(true)
Text("Non").tag(false)
}
}
}
HStack {
Picker("Have we already worked with you?", selection: $wovm.workorder.worked){
Text("Oui").tag(true)
Text("Non").tag(false)
}
}
HStack {
if wovm.workorder.worked == true {
TextField("Who serviced you?", text: $wovm.workorder.who)
}
}
}
}
.navigationBarTitle("Work Order", displayMode: .inline)
.navigationBarItems(leading:
Button(action: { self.presentationMode.wrappedValue.dismiss() }) {
Text("Cancel")
}, trailing: Button(action: {
$wovm.workorder.addwo(self.wovm.workorder)
self.presentationMode.wrappedValue.dismiss()
} ){Text("Done")})
}
}
这是它的外观(红色,空白,我不想要)。
解决方案
出现空白是因为即使 wovm.workorder.firstrepair 为假,您仍在创建 HStack。
代替:
HStack {
if wovm.workorder.firstrepair == false {
}
else {
TextField("When was the last time it happened", text: $wovm.workorder.lasttime)
.multilineTextAlignment(.trailing)
}
}
}
试试这个:
if wovm.workorder.firstrepair == true {
HStack {
TextField("When was the last time it happened", text: $wovm.workorder.lasttime)
.multilineTextAlignment(.trailing)
}
}
这样,您仅在需要基于 if 条件时才创建 HStack。
推荐阅读
- html - 我需要编写什么 HTML 或 CSS 才能使占位符属性的文本可以用鼠标选择/突出显示?
- javascript - 如何在反应中呈现有关道具更改的返回数据
- ansible - 如何在ansible playbook中使用文件的行作为变量?
- uml - 真的需要我的用例图的建议。我正在尝试为下一个目标建模一个用例:
- python - Anaconda 安装的 cudatoolkit 和 cudnn 会影响我当前的配置吗?
- python - 如果索引不同,如何停止 Pandas 中的 Shifting 列
- artifactory - 从 jcenter 下载时出现工件证书错误
- spring - 全栈应用程序 - 在哪里填写非用户定义的字段?| 正面还是背面?
- redis - 从服务器获取 redis 密钥
- android - 如何让 Apps Link Assistant 打印到 Android Studio 中的 Logcat?