I was hoping to leverage the onAppear() and onDisappear() events associated with swiftUI views, but I'm not seeing consistent behavior when using those events. The behavior can change depending on view modifiers as well as the simulator on which I run the app.
In the example code listed below, I would expect that when ItemListView2 appears, I would see the following printed out in the console:
Code Block button init button appear
And on the iPhone 8 simulator, I see exactly that.
However, on an iPhone 12 simulator, I see:
Code Block button init button appear button disappear button appear
Things get even weirder when I enable the listStyle view modifier:
Code Block button init button appear button disappear button appear button disappear button appear button appear
The iPhone 8, however remains consistent and produces the expected result.
I should also note that in no case, did the Button ever seem to disappear and re-appear to the eye.
These inconsistencies are also not simulator only issues, i noticed them on devices as well.
I need to reliably track these appear/disappear events. For example I'd need to know when a cell in a list appears (scrolled into view) or disappears (scrolled out of view) or when, say a user switches tabs.
Has anyone else noticed this behavior? To me this seems like a bug in SwiftUI, but I'm not certain as I've not used SwiftUI enough to trust myself to discern a programmer error from an SDK error. If any of you have noticed this, did you find a good work-around / fix?
Thanks,
Norm
Code Block // Sample code referenced in explanation // Using Xcode Version 12.1 (12A7403) and iOS 14.1 for all simulators import SwiftUI struct ItemListView2: View { let items = ["Cell 1", "Cell 2", "Cell 3", "Cell 4"] var body: some View { ListingView(items: items) } } private struct ListingView: View { let items: [String] var body: some View { List { Section( footer: FooterButton() .onAppear { print("button appear") } .onDisappear { print("button disappear") } ) { ForEach(items) { Text($0) } } } // .listStyle(GroupedListStyle()) } } private struct FooterButton: View { init() { print("button init") } var body: some View { Button(action: {}) { Text("Button") } } }