简体   繁体   English

从 DatePicker SwiftUI 更改选定的日期格式

[英]Change selected date format from DatePicker SwiftUI

Is there a way to format the date?(from the position indicated by the arrow in the picture) I know it is formatted based on the locale but is there a way to format it myself?有没有办法格式化日期?(从图中箭头指示的位置)我知道它是根据语言环境格式化的,但是有没有办法自己格式化呢?

在此处输入图像描述

struct ContentView: View {

    @State private var selectedDate = Date()

    var body: some View {
        Form {

            DatePicker(selection: $selectedDate, in: ...Date(), displayedComponents: .date) {
                Text("From*")
            }
        }
    }
}

The only way I could figure to accomplish this is to create my own custom DatePicker view and use onAppear on the TextField to update an @State selectedDateText: String variable for displaying in the TextField.我能想到的实现此目的的唯一方法是创建我自己的自定义 DatePicker 视图,并在 TextField 上使用 onAppear 来更新 @State selectedDateText: String 变量以在 TextField 中显示。 This feels like a hack and I'm almost embarrassed to post it but it works.这感觉像是一个 hack,我几乎不好意思发布它,但它有效。 I'm new at Swift and iOS programming in general so I'm sure someone will come along with a better answer so I'll offer this for what it's worth.一般来说,我是 Swift 和 iOS 编程的新手,所以我相信有人会提出更好的答案,所以我会提供它的价值。 My custom view is something like this:我的自定义视图是这样的:

struct CustomDatePicker: View {
  @Binding var date: Date

  @State private var showPicker: Bool = false
  @State private var selectedDateText: String = "Date"

  private var selectedDate: Binding<Date> {
    Binding<Date>(get: { self.date}, set : {
        self.date = $0
        self.setDateString()
    })
  } // This private var I found… somewhere. I wish I could remember where

  // To take the selected date and store it as a string for the text field
  private func setDateString() {
    let formatter = DateFormatter()
    formatter.dateFormat = "MMMM dd, yyyy"

    self.selectedDateText = formatter.string(from: self.date)
  }

  var body: some View {
    VStack {
        HStack {
            Text("Date:")
                .frame(alignment: .leading)
            
            TextField("", text: $selectedDateText)
                .onAppear() {
                    self.setDateString()
                }
                .disabled(true)
                .onTapGesture {
                    self.showPicker.toggle()
                }
            .multilineTextAlignment(.trailing)
        }            
        
        if showPicker {
            DatePicker("", selection: selectedDate,
            displayedComponents: .date)
            .datePickerStyle(WheelDatePickerStyle())
            .labelsHidden()
        }
    }
  }
}

EDIT: I figured out where I got the private var code.编辑:我想出了我从哪里得到私人 var 代码。 It was from this post: How to detect a value change of a Datepicker using SwiftUI and Combine?它来自这篇文章: How to detect a value change of a Datepicker using SwiftUI and Combine?

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

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