In the code snippet below, .onAppear
is triggered on app launch for the 1st Text view. It fires again for the 2nd Text view when scrolled to be visible. Scrolling back to the 1st does not fire .onAppear
. .onDisappear
is not triggered at all, neither for the 1st Text view after scrolling to the 2nd nor vice versa.
Is this intended behavior?
Any ideas how to trigger .onAppear/
.onDisappear
when repeatedly scrolling the views to be visible/ invisible?
I'm using iPadOS 16.1 and Swift Playgrounds 4.2.
import SwiftUI
struct ContentView: View {
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack {
Text("1st")
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.onAppear(perform: { print("1st appeared") })
.onDisappear(perform: { print("1st disappeared") })
Text("2nd")
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.onAppear(perform: { print("2nd appeared") })
.onDisappear(perform: { print("2nd disappeared") })
}
}
}
}