TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to avoid rebuild view when tap on different button on NavigationSplitView

Forums > SwiftUI

@X901  

I have tried apple example Bringing robust navigation structure to your SwiftUI app

so my code looks like this

  NavigationSplitView(
      columnVisibility: $navigationModel.columnVisibility
  ) {
      List(
          categories,
          selection: $navigationModel.selectedCategory
      ) { category in
          NavigationLink(category.localizedName, value: category)
      }
      .navigationTitle("Categories")
      .toolbar {
          ExperienceButton(isActive: $showExperiencePicker)
      }
  } detail: {
      NavigationStack(path: $navigationModel.recipePath) {
          RecipeGrid(category: navigationModel.selectedCategory)
      }
  }

Details View

struct RecipeGrid: View {
    var category: Category?
    var dataModel = DataModel.shared

    var body: some View {
        ZStack {
            if let category = category {
                ScrollView {
                    LazyVGrid(columns: columns) {
                        ForEach(dataModel.recipes(in: category)) { recipe in
                            NavigationLink(value: recipe) {
                                RecipeTile(recipe: recipe)
                            }
                            .buttonStyle(.plain)
                        }
                    }
                    .padding()
                }
                .navigationTitle(category.localizedName)
                .navigationDestination(for: Recipe.self) { recipe in
                    RecipeDetail(recipe: recipe) { relatedRecipe in
                        NavigationLink(value: relatedRecipe) {
                            RecipeTile(recipe: relatedRecipe)
                        }
                        .buttonStyle(.plain)
                    }
                }
            } else {
                Text("Choose a category")
                    .navigationTitle("")
            }
        }
    }

    var columns: [GridItem] {
        [ GridItem(.adaptive(minimum: 240)) ]
    }
}

My issue is if I go to details view then tap on other sidebar item after that return to same tap, it will return to rootview also onAppear it toggled! that mean the view did rebuild itself

watch this video

on Apple News app it will stay on save view it won't rebuild or return to rootview when I change sidebar item

watch this the video

I want same behavior, but I don't know how can I do it

I didn't find any question here or any article explain how to do the same behavior as Apple News app

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.