Skip to content

Instantly share code, notes, and snippets.

@samsonjs
Created December 1, 2024 22:22
Show Gist options
  • Save samsonjs/37385472e084f914333baeec0aefdc9a to your computer and use it in GitHub Desktop.
Save samsonjs/37385472e084f914333baeec0aefdc9a to your computer and use it in GitHub Desktop.
SwiftUI navigation freeze when defining openURL and using a property or function to build a view
//
// ContentView.swift
// NavigationFreeze
//
// Created by Sami Samhuri on 2024-12-01.
//
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationStack {
VStack {
NavigationLink(destination: RecipesView()) {
Label("Show Recipes (freezes)", systemImage: "books.vertical")
.padding(.vertical)
}
NavigationLink(destination: RecipesViewWithoutOpenURL()) {
Label("Show Recipes (no freeze)", systemImage: "books.vertical")
.padding(.vertical)
}
NavigationLink(destination: RecipesViewWithInlineDestination()) {
Label("Show Recipes (also no freeze)", systemImage: "books.vertical")
.padding(.vertical)
}
}
.buttonStyle(.borderedProminent)
}
}
}
struct RecipesView: View {
// This only happens when we have openURL in the environment.
@Environment(\.openURL) private var openURL
var body: some View {
// Changing this to an inline view expression also makes it work for some reason.
NavigationLink(destination: recipeView) {
Text("Mom's Secret Pasta Sauce (will freeze)")
.padding(.vertical)
}
.buttonStyle(.borderedProminent)
}
private var recipeView: some View {
Text("<insert recipe here>")
}
}
struct RecipesViewWithoutOpenURL: View {
var body: some View {
NavigationLink(destination: recipeView) {
Text("Mom's Secret Pasta Sauce (will not freeze)")
.padding(.vertical)
}
.buttonStyle(.borderedProminent)
}
private var recipeView: some View {
Text("<insert recipe here>")
}
}
struct RecipesViewWithInlineDestination: View {
// This only happens when we have openURL in the environment.
@Environment(\.openURL) private var openURL
var body: some View {
NavigationLink(destination: Text("<insert recipe here>")) {
Text("Mom's Secret Pasta Sauce (will not freeze)")
.padding(.vertical)
}
.buttonStyle(.borderedProminent)
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment