NavigationBarTitle and NavigationItems does not appear in simulator, only in preview

45 Views Asked by At

I'm pretty new to swift and xcode, but i'm stuck at this problem. I have a view that contains a NavItems component, which is a custom "navbar" for the app. The issue is when i'm testing using the xcode preview it all works fine. But using the simulator or using my phone, the NavBarTitle and NavItems that contains a NavItem view does not show, it only presents the map view that i use inside the main page view.

The code for the main page view:


import SwiftUI
import MapKit

struct MainPageView: View {
    @State var showMenu = false
    @State var path = NavigationPath()

    var body: some View {
        if UserDefaults.standard.value(forKey: "login") == nil {
            LoginView()
        } else {
            NavigationStack(path: $path) {
                NavigationView {
                    GeometryReader { geometry in
                        ZStack(alignment: .leading) {
                            MainPage()
                                .frame(width: geometry.size.width, height: geometry.size.height)
                                .offset(x: self.showMenu ? geometry.size.width / 2 : 0)
                                .disabled(self.showMenu ? true : false)

                            if self.showMenu {
                                SideMenuView()
                                    .navigationBarBackButtonHidden(true)
                                    .frame(width: geometry.size.width / 1.4)
                                    .transition(.move(edge: .leading))
                            }
                        }
                        
                    }
                    .gesture(DragGesture().onChanged { _ in }
                                          .onEnded { gesture in
                                              if gesture.translation.width < -100 {
                                                  withAnimation {
                                                      self.showMenu = false
                                                  }
                                              }
                                          })
                }
                .onAppear {
                    showMenu = false
                }
                .navigationDestination(for: String.self) { view in
                    if view == "LoginView" {
                        LoginView().navigationBarBackButtonHidden(true)
                    }
                }
            }
            .background(Color.clear) // Ensure ZStack doesn't block user interaction
            .navigationBarTitle("Coonecta Client", displayMode: .inline)
            .navigationBarItems(leading: NavItems(showMenu: $showMenu))
        }
    }
}


struct NavItems : View{
    @Binding var showMenu: Bool

    var body: some View{
        Button(action: {
            withAnimation{
                self.showMenu.toggle()
            }
        }) {
            Image(systemName: "line.horizontal.3")
                .imageScale(.large)
                .foregroundColor(.gray)
        }
    }
}

struct MainPage: View {
    var body: some View {
        MapContainerView()
    }
}

struct MainPage_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView {
            MainPageView()
                .environmentObject(LoginSettings())
                .environmentObject(SideMenuSettings())
        }
    }
}

Code for the sideMenu, in case it is usefull.


import SwiftUI

struct SideMenuView: View {
    @EnvironmentObject var settings: LoginSettings
    @EnvironmentObject var sideMenu: SideMenuSettings
    
    var body: some View {
        VStack(alignment: .leading){
            HStack{
                Image("logo_topbar")
                    .resizable()
                    .frame(width: 110, height: 40)
            }
            .padding(.top, 100)
            .padding(.bottom, -70)
            HStack{
                Image(systemName: "person")
                    .foregroundColor(.gray)
                    .imageScale(.large)
                Text("\("[email protected]")")
                    .foregroundColor(.gray)
                    .font(.headline)
            }
            .padding(.top, 100)
            
            HStack{
                Text("lala")
                    .foregroundColor(.gray)
                    .font(.headline)
            }
            .padding(.top, 25)
            
            HStack{
                Button(action: {
                    UserDefaults.standard.removeObject(forKey: "login")
                    DispatchQueue.main.asyncAfter(deadline: .now()) {
                                      UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
                    }
                }) {
                    Image(systemName: "rectangle.portrait.and.arrow.right")
                        .imageScale(.large)
                        .foregroundColor(.gray)
                }
                Text("Logout")
                    .foregroundColor(.gray)
            }
            .padding(.top, 25)
            Spacer()
        }
        
    
        .padding()
        .frame(maxWidth: .infinity, alignment: .leading)
        .background(.white)
        .edgesIgnoringSafeArea(.all)
    }
}

struct SideMenuView_Previews: PreviewProvider {
    static var previews: some View {
        SideMenuView()
            .environmentObject(LoginSettings())
    }
}

I've tried calling both the navitems and the navbartitle in the preview provider, according to some solutions i found online:

.navigationBarTitle("Client", displayMode: .inline)
.navigationBarItems(leading: NavItems(showMenu: .constant(false)))

But it made it buggy and was showing the hamburger menu item twice. Also tried with simulating with different phone models, but problem still persists.

0

There are 0 best solutions below