当DatePicker在表单内部时,如何使其保持打开状态?

时间:2020-08-14 20:01:26

标签: swiftui

如何使DatePicker保持打开状态?

我将DatePicker放在Form内,所以我必须点击Form来打开DatePicker。 但是,相反,我希望它始终处于打开状态,即使用户没有点击然后单击表单。 反正会实现这一目标吗?

这是我当前的代码。

struct ContentView: View {
    @State private var selectedDate = Date()
    var body: some View {
        VStack{
            Form{
                DatePicker(selection: self.$selectedDate, in: Date()..., displayedComponents: .date, label: { Text("Select a date") })
                  }
            }
        }
    }

这是我尝试过的。使用this method

  struct ContentView: View {
    @State private var pickerReset = UUID()
    
    @State private var selectedDate = Date()
    var body: some View {
        VStack{
            Form{
                DatePicker(selection: self.$selectedDate, in: Date()..., displayedComponents: .date, label: { Text("Select a date") }).id(self.pickerReset)
                  }
        }.onAppear{
            //I tired to show datePicker with UUID(), but
            //this line of code ratherly hide the datepicker
            self.pickerReset = UUID()
        }
        }
}

1 个答案:

答案 0 :(得分:2)

您可以尝试显式设置(venv) C:\Users\edward_arrow\path_to_location>python runscript.py C:\Users\edward_arrow\path_to_location\venv\lib\site-packages\pa ndas\core\computation\expressions.py:68: FutureWarning: elementwise comparison f ailed; returning scalar instead, but in the future will perform elementwise comp arison return op(a, b)

datePickerStyle

如果需要,您还可以使用第二个禁用的struct ContentView: View { @State private var selectedDate = Date() var body: some View { VStack { Form { DatePicker("", selection: $selectedDate, in: Date()..., displayedComponents: .date) .datePickerStyle(WheelDatePickerStyle()) } } } } 来显示选定的日期-表现得更像表单中的 standard DatePicker:

DatePicker