NavigationView ignores preference keys from its root view
Originator: | chris | ||
Number: | rdar://FB9968385 | Date Originated: | 2022-03-28 |
Status: | Open | Resolved: | |
Product: | SwiftUI Framework | Product Version: | 15.4 |
Classification: | Incorrect/Unexpected Behavior | Reproducible: | Always |
NavigationView ignores preference keys from its root view when popping back to the root view. Additionally, the NavigationView returns the default value for a preference key before returning the root view's preference key value on the NavigationView's first appearance. Given: * The code below * The user input: push, push, pop, pop, push, push, pop, pop Expected output: 0, 1, 2, 1, 0, 1, 2, 1, 0 Output on iOS 15.4 (device and simulator): -1, 0, 1, 2, 1, 2, 1 Code: ```swift import SwiftUI @main struct navigation_view_preference_keyApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { var body: some View { NavigationView { LeafView(count: 0) } .onPreferenceChange(CountKey.self) { newValue in print(newValue) } } } struct LeafView: View { let count: Int var body: some View { NavigationLink("Push") { LeafView(count: count + 1) } .frame(maxWidth: .infinity, maxHeight: .infinity) .navigationTitle("\(count)") .preference(key: CountKey.self, value: count) } } struct CountKey: PreferenceKey { static var defaultValue: Int = -1 static func reduce(value: inout Int, nextValue: () -> Int) { value = nextValue() } } ``` SEO: NavigationView NavigationLink PreferenceKey onPreferenceChange preferenceKey preference(key:value:)
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!