3

我现在在里面添加了 datePicker HStack。我只想datePickertextField处于编辑模式时显示。

struct EditProfile: View {
    @Binding var profile: Profile
    var body: some View {
        List {
            HStack {
                Text("Username").bold()
                TextField("Date")
                DatePicker(
                    $profile.goalDate,
                    minimumDate: Calendar.current.date(byAdding: .year, value: -1, to: profile.goalDate),
                    maximumDate: Calendar.current.date(byAdding: .year, value: 1, to: profile.goalDate),
                    displayedComponents: .date
                )
            }
        }
        .padding(.top)
    }
    .padding(.top)
}
4

1 回答 1

1

您可以onEditingChanged与绑定一起使用以获得所需的效果。

struct EditProfile : View {
    @Binding var profile: Profile
    @State var showDatePicker = false
    @State var textfieldText: String = ""

    var body: some View {
       List {
            HStack {
                Text("Username").bold()
                TextField($textfieldText, placeholder: Text("Date"), onEditingChanged: { (editting) in
                    self.showDatePicker = editting
                }) {

                }

                if self.showDatePicker {
                    DatePicker(
                        $profile.goalDate,
                        minimumDate: Calendar.current.date(byAdding: .year, value: -1, to: profile.goalDate),
                        maximumDate: Calendar.current.date(byAdding: .year, value: 1, to: profile.goalDate),
                        displayedComponents: .date
                    )
                }
            }
        }
        .padding(.top)
    }
}
于 2019-06-07T08:30:18.257 回答