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)
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
Convert JSON String to JSON Object in Swift 4
Hide Dots from Uipageviewcontroller
Modifying Ekparticipants (Attendees) in Eventkit Like Sunrise
Repeat Image Horizontally and Vertically
Touchesbegan, Touchesended, Touchesmoved for Moving Uiview
iOS 11 Core Nfc - Any Sample Code
Error: Uiview's Window Is Not Equal to Another View's Window
Run a Swift 2.0 App Forever in Background to Update Location to Server
Error: Module File's Minimum Deployment Target Is iOS8.3 V8.3
Uitextfield Should Accept Number Only Values
"Could Not Find Any Information for Class Named Viewcontroller"
Gcd VS Performselectorinbackground/Performselectoronmainthread
How to Add My App to the Share Sheet Action
Repeating Local Notifications for Specific Days of Week (Swift 3 iOS 10)
iOS Keychain Services: Only Specific Values Allowed for Ksecattrgeneric Key