I came up with this solution independently while working on a project. After some more Googling, I found others had come across the same bug and solved it. I am using this as an opportunity to try my first CocoaPod and SwiftPackage.
The primary problem is with NavigationLink
. The destination view given will be immediately instantiated and loaded--before the user even navigates to the details page. This is a problem for large details pages that you don't want to have immediately loaded with the previous screen.
To run the example project, clone the repo, and run pod install
from the Example directory first.
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: LazyView(Text("My details page"))) {
Text("Go to details")
}
}
}
}
LazyViewSwiftUI is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'LazyViewSwiftUI'
LazyViewSwiftUI is also available through Swift Package Manager.
Once you have your Swift package set up, adding LazyViewSwiftUI as a dependency is as easy as adding it to the dependencies value of your Package.swift.
dependencies: [ .package(url: "https://github.com/joehinkle11/Lazy-View-SwiftUI") ]