-1

I am still learning SwiftUI and I have come across a small problem.

In my app, I have a main view. On the top is a search bar and at the bottom, a menu with different buttons. I want to change views when clicking those buttons. However, I only want to change the middle section.

No big deal, I will just put the middle part into a NavigationView. That works alright and I am able to change my views. My problem is that the buttons below do not have any impact on the new view.

To try to simplify: Let’s say I’m on home page. I then click the grocery list button (guess what I’m making school projects lol). My navigation link works just fine and goes to the list. So, now I’m on view 2 let’s say. When I press the home button, it doesn’t close that view and go to my main one. Here is my code setup:

import SwiftUI

struct ContentView: View {
    @State private var searchText: String = ""
    @State private var action: Int? = 0
    
    var body: some View {
    
        ZStack {
            
            // Top Menu
            
            VStack{
                HStack {
                    Spacer()
                    TextField("Search",
                              text: $searchText)
                        .background(Color.white)
                        
                    Button(action: {
                        self.action = 1
                    }, label: {
                        Image(systemName: "magnifyingglass.circle")
                            .font(.largeTitle)
                    })
                    Spacer()
                }
                
                // Body
                
                NavigationView {
                            VStack {
                                Text("Can I See Something")
                                NavigationLink(destination: SearchView(), tag: 1, selection: $action) {
                                }
                                Text("Yes/No")
                            }
                        }
                
                Spacer()
                
                // Bottom Menu
                
                HStack (alignment: .top) {
                    Spacer()
                    VStack {
                        Button(action: {
                            
                        }, label: {
                            Image(systemName: "house.fill")
                                .font(.largeTitle)
                        })
                            .padding(.top)
                        Text("Home")
                    }
                    Divider()
                        .padding(.horizontal)
                        .frame(width: 2.5, height: 100)
                    VStack {
                        Button(action: {
                            
                        }, label: {
                            Image(systemName: "newspaper")
                                .font(.largeTitle)
                        })
                            .padding(.top)
                        Text("Weekly\nAd")
                            .multilineTextAlignment(.center)
                    }
                    Divider()
                        .padding(.horizontal)
                        .frame(width: 2.5, height: 100)
                    VStack {
                        Button(action: {
                            
                        }, label: {
                            Image(systemName: "checklist")
                                .font(.largeTitle)
                        })
                            .padding(.top)
                        Text("Grocery\nList")
                            .multilineTextAlignment(.center)
                    }
                    Divider()
                        .padding(.horizontal)
                        .frame(width: 2.5, height: 100)
                    VStack {
                        Button(action: {
                            
                        }, label: {
                            Image(systemName: "person.crop.circle")
                                .font(.largeTitle)
                        })
                            .padding(.top)
                        Text("Account")
                    }
                    Spacer()
                }
            }
        }
    }
}

struct SearchView: View {
    var body: some View {
        ZStack {
            Text("Nothing to see here!")
        }
        .navigationBarBackButtonHidden(true)
    }
    
}

SearchView is a separate view (in its own file) in the app that opens up when the magnifying glass button is pressed. Currently it does not do anything. However I want to be able to press those buttons on this view above to still navigate the app.

Also, on another note, is there anyway to get rid of the back button?

ssnyder85
  • 21
  • 3
  • Hiding the back button: https://stackoverflow.com/questions/57112026/how-can-i-hide-the-navigation-back-button-in-swiftui – jnpdx Mar 09 '22 at 23:20
  • This really would benefit from a [mre] -- it's not clear what the connection is between the three lines of code that you included. – jnpdx Mar 09 '22 at 23:20
  • Thanks for the suggestion. I added the code so you could see what I have going on. I apologize for the mess it is usually cleaner than this but again I am still learning. – ssnyder85 Mar 09 '22 at 23:28
  • That isn't a minimal reproducible example. We should be able to copy your code and run it. You need to strip it of unnecessary things like `SetupColor` and just use standard colors, if necessary, and pull out things like shadows, etc, unless they are necessary to show the problem. We also need to see `SearchView()`. Basically, take the code you want to post, and put it into a new project and make sure it builds, or throws the errors you are asking your question regarding. – Yrb Mar 09 '22 at 23:40
  • There removed all the excess. – ssnyder85 Mar 10 '22 at 00:39

1 Answers1

0

In your code the buttons do not have any function. Instead of creating a tab bar on your own, I'd rather take something like:

import SwiftUI

struct ContentView: View {
    var body: some View {
        TabView {
            MainView()
                .tabItem {
                    Label("Home", systemImage: "house.fill")
                }

            NewsView()
                .tabItem {
                    Label("Weekly\nAd", systemImage: "newspaper")
                }
        
            OrderView()
                .tabItem {
                    Label("Grocery\nList", systemImage: "checklist")
                }
        
            AccountView()
                .tabItem {
                    Label("Account", systemImage: "person.crop.circle")
                }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct MainView: View {
    var body: some View {
        Text("Home View")
    }
}

struct NewsView: View {
    var body: some View {
        Text("News View")
    }
}

struct OrderView: View {
    var body: some View {
        Text("Order View")
    }
}

struct AccountView: View {
    var body: some View {
        Text("Account View")
    }
}

In that case you'll have to create a view for each tab you are configuring (see the last 4 structs).

If you want to do it with a Stack with your own created buttons, I think you should create al 4 views as well and then you either hide them or put them out of focus by using an offset. In that case the buttons should hide/show the specific views or change the offset accordingly to move the specific views into the visible area. With the offset you also can add some animation.

Regarding the search bar on top of your app, since the views are all different, I wouldn't keep the same search bar everywhere, but if you really want to have it that way, you can embed the code + your search bar into a VStack (as you did it in your example).

Sebastian Fox
  • 1,324
  • 1
  • 10
  • 20