LAST CHANCE: Save 50% on all my Swift books and bundles! >>

SOLVED: Hiding bottom toolbar when moving to a new view

Forums > SwiftUI

Hi all,

I have a button at the bottom toolbar in my app, and the entire view is encapsulated in a NavigationStack. However, when moving to a new view the toolbar persists. How can I hide the toolbar when moving to a new view via NavigationLink?

Thanks!

2      

Hi! Would be uselful to provide your code to see where the issue might be. The below works as expected.

struct ContentView: View {
    var body: some View {
        NavigationStack {
            NavigationLink("Go to detail view") {
                DetailView()
            }
            .toolbar {
                ToolbarItem(placement: .bottomBar) {
                    Button(action: {}) {
                        Image(systemName: "house")
                    }
                }
            }
        }
    }
}

struct DetailView: View {
    var body: some View {
        Text("Detail View")
    }
}

3      

Here is the specific view in question:

import SwiftUI

struct MealListView: View {
    @State private var meals = [MealEntry]()
    @State private var searchString = ""
    @State private var showingFaves = false
    @StateObject var favorites = Favorites()

    var searchResults : [MealEntry] {
        if searchString.isEmpty {
            return meals
        }
        else {
            return meals.filter({ $0.strMeal.localizedCaseInsensitiveContains(searchString) })
        }
    }

    var body: some View {
        if !showingFaves {
            NavigationStack {
                List(searchResults, id: \.idMeal) { item in
                    let foodImage = URL(string: item.strMealThumb)!
                    NavigationLink(destination: RecipeView(currentMeal: item, mealID: item.idMeal).navigationTitle(item.strMeal).environmentObject(favorites)) {
                        HStack {
                            AsyncImage(url: foodImage, scale: 30.0){ image in image.resizable() } placeholder: { Color.gray } .frame(width: 75, height: 75) .clipShape(RoundedRectangle(cornerRadius: 10))
                            Text(item.strMeal)
                            if favorites.contains(item) {
                                Spacer()
                                Image(systemName: "heart.fill")
                                    .accessibilityLabel("Favorite this recipe!")
                                    .foregroundColor(Color.red)
                                    .onTapGesture {
                                        favorites.remove(item)
                                    }
                            }
                            else {
                                Spacer()
                                Image(systemName: "heart")
                                    .accessibilityLabel("Favorite this recipe!")
                                    .foregroundColor(Color.red)
                                    .onTapGesture {
                                        favorites.add(item)
                                    }
                            }
                        }
                    }
                }
                .navigationTitle(Text("Choose a recipe!"))
                .searchable(text: $searchString,  placement: .navigationBarDrawer(displayMode: .automatic), prompt: "Search for recipe...")
                .task {
                    await loadList()
                }
            }
            .environmentObject(favorites)
            .onDisappear {
                favorites.load()
            }
        }
        else {
            NavigationStack {
                List(searchResults, id: \.idMeal) { item in
                    if favorites.contains(item) {
                        let foodImage = URL(string: item.strMealThumb)!
                        NavigationLink(destination: RecipeView(currentMeal: item, mealID: item.idMeal).navigationTitle(item.strMeal).environmentObject(favorites)) {
                            HStack {
                                AsyncImage(url: foodImage, scale: 30.0){ image in image.resizable() } placeholder: { Color.gray } .frame(width: 75, height: 75) .clipShape(RoundedRectangle(cornerRadius: 10))
                                Text(item.strMeal)

                                Spacer()
                                Image(systemName: "heart.fill")
                                    .accessibilityLabel("Favorite this recipe!")
                                    .foregroundColor(Color.red)
                                    .onTapGesture {
                                        favorites.remove(item)
                                    }
                            }
                        }
                    }
                }
                .navigationTitle(Text("Choose a recipe!"))
                .searchable(text: $searchString,  placement: .navigationBarDrawer(displayMode: .automatic), prompt: "Search for recipe...")
            }
            .environmentObject(favorites)
            .onDisappear {
                favorites.load()
            }
        }
        Button(showingFaves ? "Show all recipes" : "Show favorites") {
            showingFaves.toggle()
        }
    }

    func loadList() async {
        guard let url = URL(string: "https://themealdb.com/api/json/v1/1/filter.php?c=Dessert") else {
            print("Invalid URL")
            return
        }
        do {
            let (data, _) = try await URLSession.shared.data(from: url)
            if let decodedResponse = try? JSONDecoder().decode(MealResult.self, from: data) {
                meals = decodedResponse.meals
            }
        } catch {
            print("Invalid data")
        }
    }
}

Here is the git repo: https://github.com/aabagdi/RecipeBrowser/tree/main

2      

But you don't have .toolbar placed in your view. You just placed it as button at the bottom. You need to place it inside both of your NavigationStacks

  .toolbar {
      ToolbarItem(placement: .bottomBar) {
          Button(showingFaves ? "Show all recipes" : "Show favorites") {
              showingFaves.toggle()
          }
      }
  }
  if !showingFaves {
            NavigationStack {
            // toolbar placed here
            }

  else {
        NavigaitonStack {
        // toolbar placed here
        }

3      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free 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.