导航栏显示模式在 SwiftUI 中无法正常工作

Navigation bar Display Mode is not working Perfectly in SwiftUI

我正在使用版本 11.3.1 (11C504) 的 SwiftUI。

我在屏幕上实现了标签栏,该屏幕将在从登录屏幕导航后出现。

如果导航 displayMode.automatic,那么当我向上滚动列表时,我会显示一个白色的 space(图片中带有黑线边框)。否则导航标题显示。 当我设置 displayMode.inline 时,它工作得很好。当我 运行 一个项目这么多次时,有时它完美无缺,有时显示 space.

我在黑边的图片中提到了白色space。

//ContentView

struct ContentView: View {

    var body: some View {
        HomeTabView()
    }
}

//HomeTabView

struct HomeTabView: View {

    @State private var selection = 0

    //Inclose user intraface of tab View.
    var body: some View {

        TabView(selection: $selection){

            TestListView().tabItem {
                Image(systemName: "book.fill")
                Text("Learn")
            }.tag(0)

            Text("Community").tabItem {
                Image(systemName: "globe")
                Text("Community")
            }.tag(1)

            //Add Notification List on the Screen.
            Text("Notification").tabItem {
                Image(systemName: "bell.fill")
                Text("Notification")

            }.tag(3)

            //Add Account  on the Tab Bar
            Text("Account").tabItem {
                Image(systemName: "person.circle.fill")
                Text("Account")
            }.tag(4)

        }.accentColor(.pink)

            .navigationBarTitle("SwiftUI")
    }

}

测试列表视图

struct TestListView: View {

    var body: some View {
            VStack{

                List(1...10, id: \.self){ num in
                    ListCards()
                }
              }.edgesIgnoringSafeArea(.all)
       }
}

//ListCards

struct ListCards: View {

    var body: some View {

        ZStack{
        RoundedRectangle(cornerRadius: 16)
                .frame(height: 180)
                .foregroundColor(.white)
                .shadow(radius: 5)

            VStack(alignment: .leading, spacing: 10){

                HStack(alignment: .top){

                    Rectangle()
                        .frame(width: 100, height: 100)
                        .cornerRadius(16)
                        .foregroundColor(.pink)

                    VStack(alignment: .leading, spacing: 4){
                        Text("SwiftUI")
                            .font(.title)

                        Text("Description of title")
                            .foregroundColor(.gray)
                    }
                }
                .padding()

            }.padding(.leading, 2)
        }.padding(.all, 6)
    }
}

您需要这样更改您的 HomeTabView:


var body: some View {

        TabView(selection: $selection) {
            NavigationView {
                TestListView()
                .navigationBarTitle("SWIFTUI")
            }.tabItem {
                Image(systemName: "book.fill")
                Text("Learn")
            }.tag(0)

            NavigationView {
                Text("Community")
            }.tabItem {
                Image(systemName: "globe")
                Text("Community")
            }.tag(1)

            //Add Notification List on the Screen.
            NavigationView {
                Text("Notification")
            }.tabItem {
                Image(systemName: "bell.fill")
                Text("Notification")

            }.tag(3)
            //Add Account  on the Tab Bar
            NavigationView {
                Text("Account")
            }.tabItem {
                Image(systemName: "person.circle.fill")
                Text("Account")
            }.tag(4)

        }.accentColor(.pink)

    }

另外,从 TestListView

中删除 .edgesIgnoringSafeArea(.all)