0
votes

I'm trying to change the navigation bar title and leading & trailing items in a SwiftUI app. Here is the situation: A user logs in, then he gets transferred to a view inside a TabView. Before login, in the welcome screen and login & register screens I have no navigation bar title, but after login, I want to add the navigation bar items and title. What is important to know is that my after-login-view is inside a TabView. If I wrap my after-login-view in a new NavigationView and then change the navigation bar items and title it works! but it shows me 2 navigation bars: the top is the first navigation bar and the bottom is the new navigation bar

which is not what I want.

here is the code I use : This is the after-Login-View

       import SwiftUI

       struct DiscoveryView: View {
             var body: some View {
                  List{
                       Text("List Items")
                  }.offset(y: 10)
                   .navigationBarTitle("", displayMode: .inline)
                   .navigationBarItems(leading:
                     Text("Discovery").font(Font.custom("Quicksand-             Bold", size: 24))
                   , trailing:
                       Button(action: {

                       }) {
                          HStack{
                             Text("120").font(Font.custom("Quicksand-Bold", size: 15)).foregroundColor(Color("BlueColor"))
                          Text("following").font(Font.custom("Quicksand-Bold", size: 15)).foregroundColor(Color.black)
                         }
                    }
                  ).navigationBarBackButtonHidden(true)
                }
              }

This is the TabView:

struct TabController : View {

@State private var selection = 0
var body: some View {
    TabView(selection: $selection){

        DiscoveryView()
            .tabItem {
                VStack {
                    Image(systemName: "list.dash")
                    Text("Discover")
                }
        }.tag(1)

        ExploreView()
            .tabItem {
                VStack {
                    Image(systemName: "square.and.pencil")
                    Text("Explore")
                }
        }.tag(2)
        SelfProfileView()
            .tabItem{
                VStack {
                    Image(systemName: "person.circle")
                    Text("Profile")

                }
        }.tag(3)

    }.accentColor(Color("BlueColor"))
}

}

This is the code of the login page:

import SwiftUI

struct LoginView: View {

var body: some View {
    ZStack{
        VStack{
            Image("mainLogo").resizable().frame(width: 250, height: 125)
            Text("").frame(width: UIScreen.main.bounds.width, height: 100)
            HStack{
                TextField("Username / Email", text: $email)
                    .padding(.leading, 10)
                    .frame(width: 313, height: 49)
                    .background(RoundedRectangle(cornerRadius: 4).stroke( Color.black.opacity(0.3)).frame(width: 313, height: 39).background(Color("GrayColor")))
            }
            Text("").frame(width: UIScreen.main.bounds.width, height: 40)
            HStack {
                HStack{
                        if self.visable{
                            TextField("Password", text: $password)
                                .padding(.horizontal)

                        } else {
                            SecureField("Password", text: $password)
                                .padding(.horizontal)

                        }
                        Button(action: {
                            self.visable.toggle()
                        }){
                            Image(systemName: self.visable ? "eye.slash" : "eye")
                                .padding(.trailing, 20)
                                .foregroundColor(Color.black)
                        }
                }.background(RoundedRectangle(cornerRadius: 4).stroke( Color.black.opacity(0.3)).frame(width: 313, height: 39).background(Color("GrayColor")) )
            }.padding(.horizontal, 40).padding(.vertical)
            Text("").frame(width: UIScreen.main.bounds.width, height: 100)
            Button(action: {
                //pass email password and conpassword to cognito
                DSManager().signIn(username: self.email, password: self.password, error: {
                    error in
                    if let error = error{
                        self.error = error.errorString
                        self.alert.toggle()
                        print(error.errorString)
                    }
                    else{
                        DSManager().getSelfUserInformation(response: {
                            userInfo, userCollections,dsError in
                            if let dsError = dsError{
                                self.error = dsError.errorString
                                self.alert.toggle()
                                print(dsError.errorString)
                            }
                            if let userInfo = userInfo{
                                print("success")
                                //use userInfo to load stuff for profile page
                                if let userCollections = userCollections{
                                    self.profileInfo.setProperties(userInfo: userInfo, collections: userCollections)
                                }
                                else{
                                    self.profileInfo.setProperties(userInfo: userInfo, collections: [:])
                                }
                                self.isComplete.toggle()

                            }
                        })
                    }
                })
            }) {
                Text("Login")
                    .fontWeight(.semibold)
                    .frame(width: 313, height: 48)
                    .background(fillAllFields() ? Color("YellowColor") : Color.gray)
                    .foregroundColor(Color.white)
                    .font(.system(size: 17))
                    .cornerRadius(15)
                    .shadow(color: Color("GrayColor"), radius: 0, x: 3, y: 3)
            }.disabled(!fillAllFields())
            NavigationLink(destination: ProfileView(), isActive: $isComplete) {
                EmptyView()
            }
            Spacer()
        }
        .padding()
        .navigationBarTitle("", displayMode: .inline)
        if self.alert {
            ErrorView(err: $error, alert: $alert)
        }
    }
}

func fillAllFields() -> Bool {
    if self.email == "" || self.password == "" {
        return false
    }
    return true
}

}

As mentioned, I'm trying to edit bar items and title of a view inside and tab view .

Thanks!

1
just noticed that when I go to after-login-view without TabView all the changes to the NavigationBar works. what am I missing?TheMachineX
Do you have a central App State which you change after user is logged in. Because if you do that yo will only have one Navigation View, inside this navigation view you will have after login or before login view. I am not sure if I understood your problem here but what I am trying to say is that you are pushing a Navigation View inside a Navigation View resulting in two navigation bars. If you can add a gif/video for better understanding of your app flow, that would be great.user832
@user832 thanks for the response but this is not my problem, but a way I tried fixing it. the problem is that I add navigationbaritems and navigationbartitle to a view that is inside a TabView but it won't show and when I load the view without the TabView the navigationBarItems and Title I adde appearTheMachineX
I see. Can you add whole code for DiscoveryView or ExploreView or SelfProfileView, in whichever you are facing issue. I see you added code for after-Login-View, but that code looks incomplete.user832
@user832 I've added some code you can go ahead and take a lookTheMachineX

1 Answers

0
votes

So as per your updated question, I added DiscoveryView Inside NavigationView and it worked as expected. I have added code and screenshot as well.

struct TabController : View {

@State private var selection = 0

var body: some View {
    TabView(selection: $selection) {
        NavigationView {
        DiscoveryView()
        }
            .tabItem {
                VStack {
                    Image(systemName: "list.dash")
                    Text("Discover")
                }
        }.tag(1)

        Text("Explore View")
            .tabItem {
                VStack {
                    Image(systemName: "square.and.pencil")
                    Text("Explore")
                }
        }.tag(2)
        Text("SelfProfileView")
            .tabItem{
                VStack {
                    Image(systemName: "person.circle")
                    Text("Profile")

                }
        }.tag(3)

    }.accentColor(Color("BlueColor"))
}} 

DiscoveryView with navigation leading and trailing view