我现在在HStack
内添加了datePicker。我只想在datePicker
处于编辑模式时显示textField
。
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)
}
答案 0 :(得分: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)
}
}