Adding a TabView makes the Navigation Bar not cover the safe area in SwiftUI

你离开我真会死。 提交于 2021-01-21 08:25:41

问题


When adding a TabView in my SwiftUI iOS App, the Navigation Bar stops covering up the notch

I've tried creating another file for the TabView implementation ( Modifying SceneDeletage and so on)

Here is a simple code without TabView that makes the Navigation Bar cover the safe area (aka the notch)

import SwiftUI

struct ContentView: View {
    var body: some View {

        NavigationView{
            ScrollView{
                HStack{
                    VStack{
                        ForEach((1...10), id: \.self){_ in
                            Text("Hello")
                            .padding(.leading, 20)
                        }
                    }
                    Spacer()
                    //.padding(.leading, 20)
                }
            }
        .navigationBarTitle("Title Covers Safe Area")
        }

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

Here is a code with TabView that makes the Navigation Bar NOT cover up the safe area

import SwiftUI

struct ContentView: View {
    var body: some View {

        TabView {
            NavigationView{
                ScrollView{
                    HStack{
                        VStack{
                            ForEach((1...10), id: \.self){_ in
                                Text("Hello")
                            }
                        }
                        Spacer()
                    }
                    .padding(.leading, 20)
                }
                .navigationBarTitle("Doesn't Cover Safe Area")
            }
                .tabItem {
                    Image(systemName: "1.circle")
                    Text("First")
                }.tag(0)
            HStack{
                Spacer()
                VStack{

                    Spacer()
                    Text("Second View")
                        .font(.system(size: 40))
                }

            }

                .tabItem {
                    Image(systemName: "2.circle")
                    Text("Second")
                }.tag(1)
        }

    }
}


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


回答1:


You can use method edgesIgnoringSafeArea(_:)

TabView {
  ...
}
.edgesIgnoringSafeArea(.top)


来源:https://stackoverflow.com/questions/58173696/adding-a-tabview-makes-the-navigation-bar-not-cover-the-safe-area-in-swiftui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!