Home > front end >  iOS SwiftUI - Remove top space and back button doesn't work
iOS SwiftUI - Remove top space and back button doesn't work

Time:09-04

var body: some View {
    
    NavigationView{
        ZStack(alignment: .leading){
            
            ZStack(alignment: .top){
                VStack{
                    HStack(alignment: .top) {
                        Image(systemName: "chevron.left")
                            .font(.title2)
                        Text("profile")
                            .fontWeight(.bold)
                        
                        Spacer()
                        
                        Button(action: {
                            

                        }) {

                            Image(systemName: "person.crop.circle.fill.badge.plus")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .frame(height: 24)
                            
                            Text("subscribe")
                            
                            Button(action: {

                            }) {
                                Image("share_black_24dp")
                                    .tint(Color.white)
                                    .foregroundColor(Color.white)
                            }
                            
                            
                            
                        }
                        .foregroundColor(Color.white)

                    }
                    .frame(width: UIScreen.main.bounds.width - 20, height: 40)
                    .padding(.horizontal, 20)
                    .background(Color.teal)
                    
                }

                // .navigationBarItems(leading: backButton)

                ScrollView(.vertical, showsIndicators: false){
                    
                }
                
            }
        }
        .navigationBarTitle("")
        .navigationBarHidden(true)
        .navigationBarBackButtonHidden(true)
    }
}

where does

    .navigationBarTitle("")
    .navigationBarHidden(true)
    .navigationBarBackButtonHidden(true)

need to be!?

I also tried it for NavigationView and it doesn't work

CodePudding user response:

Your issue is that you have multiple NavigationViews.

NavigationView creates a new stack of views. Only your main view should have a NavigationView. The child views can use NavigationLink to push new views, but they should not create another NavigationView.

Think of it like this. NavigationView creates that stack to hold the views, and NavigationLink pushes a new view onto the stack. You only want to create one stack.

So, get rid of NavigationView in your View above, because it is linked from the main view which creates the NavigationView. Then, your code to hide the navigation bar will work as you expect.

  • Related