I have one parent View which has two Child views.
When the child view changes the preference value, the parent receives the preference changes via onPreferenceChange
However, when the parent has a Date Picker, onPreferenceChange doesn't get called and stops working.
Has anyone found any workaround for this ?
struct parentView: View {
@State private var date = Date()
var body: some View {
VStack{
ChildView1()
Text("Child View 2")
// DatePicker("", selection: self.$date, displayedComponents: .hourAndMinute)
}
.onPreferenceChange(testPreference.self, perform: { value in
print("Printing preference value")
print(value)
})
}
}
struct ChildView1: View {
@State private var tablets : Int = 0
var body: some View {
Text("Child View 1")
.onTapGesture {
self.tablets = self.tablets + 1
}
.preference(key: testPreference.self, value: self.tablets)
}
}
struct testPreference: PreferenceKey {
typealias Value = Int
static var defaultValue: Int = 0
static func reduce(value: inout Int, nextValue: () -> Int) {
value = nextValue()
}
}
.onPreferenceChangefrom theVStacktoChildView1(). Even putting it on theTextbizarrely doesn't work. Burying the DatePicker in a sub-container also doesn't work. It feels like it's pointing to some kind of hack inside of DatePicker.