简体   繁体   English

仅允许在 datePicker (SwiftUI) 上选择单个日期

[英]Only allowing individual dates to be selected on a datePicker (SwiftUI)

I am making a project with a date picker form that I only want users to be able to select from a certain group of dates that is stored as an array (with no particular pattern).我正在制作一个带有日期选择器表单的项目,我只希望用户能够从存储为数组(没有特定模式)的特定日期组中 select 。 How could I disable (or enable) only certain dates on the datePicker.如何仅禁用(或启用)datePicker 上的某些日期。 (possibly with a visual cue to show that certain dates are disabled) Thanks in advance. (可能带有视觉提示以显示某些日期已禁用)提前致谢。

Try:尝试:

struct ContentView: View {

    var dates = ["Date 1", "Date 2", "Date 3", "Date 4"]
        var grayedDates = ["Date 2", "Date 4"]
    
        var body: some View {
            VStack {
                Picker("Please choose a color", selection: $selectedColor) {
                    ForEach(dates, id: \.self) {
                        
                        if grayedDates.contains($0) == true {
                            Text("Unavailable: " + $0)
                        } else {
                            Text($0)
                        }
                       
                        /*
                        //but to gray out elements, this could have been a solution, if someone knows why it won't work...update:
                        Text($0)
                            .foregroundColor(grayedDates.contains($0) == true ? .gray : .black)
                         */
                    }
                }
            }
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM