Swiftui Datepicker Jumps Between Short and Medium Date Formats When Changing the Date

SwiftUI DatePicker jumps between short and medium date formats when changing the date

Looks like a bug. Here is a found workaround (tested with Xcode 13beta / iOS 15)

demo

  DatePicker(
"Date",
selection: $date,
displayedComponents: .date
)
.labelsHidden()
.id(date) // << here !!

How to change the date format of the showcase text of DatePicker in SwiftUI iOS14

I had the same problem. I solved it by adding .id

Example:

DatePicker(selection: $dob, in: ...Date(), displayedComponents: [.date]) {
HStack(spacing: 0) {
Text("Select birthday ")
Image(systemName: "arrow.right")
}
.foregroundColor(Color("textfield"))
}
.id(dob)

This prevents the date picker from being created more than once.

Source: SwiftUI DatePicker jumps between short and medium date formats when changing the date

SwiftUI: unfocus TextField when DatePicker date has been chosen

Try computable binding with side effect.. this should be activated only from within DataPicker:

DatePicker("",
selection: Binding(get: {selectedDate}, set:{
selectedDate = $0
UIApplication.shared.endEditing() // << here !!
}),
displayedComponents: .date)
.padding(.horizontal)
.frame(width: 333)
.datePickerStyle(.graphical)
.labelsHidden()

Fetching and setting date from datepicker, but still getting old default value

Reply against your second question about wrapper properties used in SwiftUI i.e @State, @Binding, @Published.

The most common @Things used in SwiftUI are:

•   @State - Binding<Value>
• @Binding - Binding<Value>
• @ObservedObject - Binding<Value> (*)
• @EnvironmentObject - Binding<Value> (*)
• @Published - Publisher<Value, Never>

(*) technically, we get an intermediary value of type Wrapper, which turns a Binding once we specify the keyPath to the actual value inside the object.
So, as you can see, the majority of the property wrappers in SwiftUI, namely responsible for the view’s state, are being “projected” as Binding, which is used for passing the state between the views.
The only wrapper that diverges from the common course is @Published, but:
1. It’s declared in Combine framework, not in SwiftUI
2. It serves a different purpose: making the value observable
3. It is never used for a view’s variable declaration, only inside ObservableObject
Consider this pretty common scenario in SwiftUI, where we declare an ObservableObject and use it with @ObservedObject attribute in a view:

class ViewModel: ObservableObject {
@Published var value: Int = 0
}

struct MyView: View {
@ObservedObject var viewModel = ViewModel()

var body: some View { ... }
}

MyView can refer to $viewModel.value and viewModel.$value - both expressions are correct. Quite confusing, isn’t it?
These two expressions ultimately represent values of different types: Binding and Publisher, respectively.
Both have a practical use:

var body: some View {
OtherView(binding: $viewModel.value) // Binding
.onReceive(viewModel.$value) { value // Publisher
// do something that does not
// require the view update
}
}

Hope it may help you.



Related Topics



Leave a reply



Submit