0

I hide the navigation bar and provide a customized navigation bar. But my list appears below the navigation bar. I want the list to appear below the navigation bar when scrolling.

My desired effect:

enter image description here

Actual effect: Blocked 0

enter image description here


import SwiftUI

struct ContentView: View {
    
    var body: some View {
        NavigationView {
            TabView {
                FirstView()
                    .tabItem {
                        Image(systemName: "folder.fill")
                        Text("Home")
                    }
                
                SecondView()
                    .tabItem {
                        Image(systemName: "folder.fill")
                        Text("SecondView")
                    }
            }
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}

struct FirstView: View {
    
    var body: some View {
        ZStack(alignment: .top) {
            List {
                
                ForEach(0..<40, id: \.self) { index in
                    Text("cell view \(index)")
                }
            }
            .listStyle(.inset)
            
            HStack {
                Text("首页")
            }
            .frame(maxWidth: .infinity)
            .frame(height: 44)
            .background(.bar)
        }
        .navigationBarTitleDisplayMode(.inline)
        //.ignoresSafeArea(edges: .)
    }
}

struct SecondView: View {
    
    var body: some View {
        List {
            ForEach(0..<40, id: \.self) { index in
                Text("FirstView2")
            }
        }
        .listStyle(.inset)
    }
}

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



Please do not add padding to the List. This is a security zone problem.

gaohomway
  • 2,132
  • 1
  • 20
  • 37
  • I found this link, which can be the opposite of our problem https://stackoverflow.com/questions/57517803/how-to-remove-the-default-navigation-bar-space-in-swiftui-navigationview – gaohomway Oct 29 '22 at 04:31

1 Answers1

0

Edit: scrolling under the header

For a glass effect under the header:

You can add a "dummy" element that will push the list one row down. So the the header will cover the dummy element and the whole list will be visible.

struct FirstView: View {
    
    var body: some View {
        ZStack(alignment: .top) {
            List {
                
                // Dummy Text
                Text("")
                
                ForEach(0..<40, id: \.self) { index in
                    Text("cell view \(index)")
                }
            }
            .listStyle(.inset)
            
            HStack {
                Text("首页")
            }
            .frame(maxWidth: .infinity)
            .frame(height: 44)
            .background(.bar)
            .opacity(0.7)

        }
        .navigationBarTitleDisplayMode(.inline)
        //.ignoresSafeArea(edges: .)
    }
}

Original answer

Your FirstView is using a ZStack, which tells exactly the compiler to show one view on top of (covering) the other. Use a VStack, that should solve your issue.

struct FirstView: View {

    var body: some View {
        VStack {
            HStack {
                Text("首页")
            }
            .frame(maxWidth: .infinity)
            .frame(height: 44)
            .background(.bar)

            List {
                
                ForEach(0..<40, id: \.self) { index in
                    Text("cell view \(index)")
                }
            }
            .listStyle(.inset)
            
        }
        .navigationBarTitleDisplayMode(.inline)
        //.ignoresSafeArea(edges: .)
    }
}
HunterLion
  • 3,496
  • 1
  • 6
  • 18
  • Thanks for the reply, but no, I need to scroll, and I want the content to appear under the navigation bar. It has the effect of film glass. – gaohomway Oct 29 '22 at 04:55
  • I posted another option. It's a dummy solution but I hope it works. – HunterLion Oct 29 '22 at 05:04
  • Can solve the initial problem, but if I use the pull-down refresh control `refreshable` will also be hidden – gaohomway Oct 29 '22 at 05:07
  • Look at the comment link below my question, I don't know why, his question will be filled by default, mine is not filled by default, I am using xcode14, the simulator is also iPhone14 iOS16 – gaohomway Oct 29 '22 at 05:09