简体   繁体   中英

SwiftUI onDisappear not being called

I am using a ScrollView and VStack to present bunch of views in a list. But for some reason the onDisappear is not being called, the onAppear was though.

Does onDisappear only call when that view has disappeared off the screen? Or when it's deinitialized

struct AView: View {
        
    var body: some View {
        ScrollView(.vertical, showsIndicators: false) {
            VStack {
                ForEach(0...99, id: \.self) { _ in
                    SomeView()
                        .onDisappear {
                            print("Disappeared")
                        }
                }
            }
        }
    }
}

When you're using VStack , it's gonna draw all the views inside - all of them are added to the view tree, it doesn't matter wether it's visible on the screen.

If you replace it with LazyVStack it'll make content lazy - only visible views will be added to the view tree and onDisappear will be called as you expect it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM