使用if-else根据状态显示时,SwiftUI视图闪烁

时间:2020-08-06 17:53:56

标签: swift swiftui

我想基于Picker选定的值在应用程序中显示某种形式。但是,当我足够快地使用分段控件进行切换时(这里似乎是强制的,但是当表单更复杂时,它就非常明显)。编辑:这似乎发生在所有View上,而不仅仅是表格。

import SwiftUI

struct ContentView: View {
    
    @State private var calculationType = CalculationType.months
    
    @State private var balanceOwned: String = ""
    
    var body: some View {
        VStack {
            Picker("Calculation Type", selection: $calculationType) {
                ForEach(CalculationType.allCases, id: \.self) {
                    Text($0.rawValue.capitalized)
                }
            }.pickerStyle(SegmentedPickerStyle())

            // This check seems to be the cause of the problem!
            if calculationType == .months {
                CustomForm(balanceOwned: $balanceOwned)
            } else if calculationType == .fixed {
                CustomForm(balanceOwned: $balanceOwned)
            } else {
                CustomForm(balanceOwned: $balanceOwned)
            }
        }
    }
}

struct CustomForm: View {
    
    @Binding var balanceOwned: String
    
    var body: some View {
        Form {
            Section {
                TextField("Test", text: $balanceOwned)
                    .foregroundColor(.white)
            }
        }
    }
}

enum CalculationType: String, CaseIterable {
    case months
    case fixed
    case minimum
}

表格中的所有内容都会闪烁。我该如何解决?

0 个答案:

没有答案