首页 > 解决方案 > 未触发时,表单中的 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")})
    }
}

这是它的外观(红色,空白,我不想要)。

问题是如何出现的

标签: xcodeswiftui

解决方案


出现空白是因为即使 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。


推荐阅读