首页 > 解决方案 > SwiftUI NavigationView 未在视图中完全扩展

问题描述

我正在使用 SwiftUI 中的 NavigationView,并且遇到了将其完全扩展到屏幕底部的问题。

我在其中创建了一个简单的列表,效果很好。但是,当我将它放在 NavigationView 中时,它会在底部创建一个灰色区域。我试过调整框架和其他一些东西都无济于事。我以前从未见过这种情况。任何帮助,将不胜感激。

简单列表视图

添加到导航视图

struct ListingView: View {
    var body: some View {
        List(0..<5) { item in
            Text("Test")
        }
    }
}

struct ListingView: View {
    var body: some View {
        NavigationView {
            List(0..<5) { item in
                Text("Test")
            }
        }
    }
}

struct ContentView: View {
    // PacificBlue Background Set Up.
    init() {
        UITabBar.appearance().isTranslucent = false
        UITabBar.appearance().barTintColor = UIColor(Color.pacificBlue)
    }
    
    // MARK: View
    var body: some View {
        TabView {
            SeafoodListView()
                .tabItem {
                    Image(systemName: "line.diagonal.arrow")
                    Text("Market")
                }.tag(0) // SeafoodMarketView
            ListingView()
                .tabItem {
                    Image(systemName: "list.bullet.rectangle")
                    Text("Listings")
                }.tag(1) // ListingView
            RequestView()
                .tabItem {
                    Image(systemName: "megaphone")
                    Text("Requests")
                }.tag(2) // RequestView
            MessengerView()
                .tabItem {
                    Image(systemName: "ellipsis.bubble")
                    Text("Messenger")
                }.tag(3) // MessengerView
            AccountView()
                .tabItem {
                    Image(systemName: "person")
                    Text("Account")
                }.tag(4) // AccountView
        } // TabView
        .accentColor(.white)
    }
}

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

标签: swiftuiswiftui-navigationview

解决方案


这是由于外观黑客...

init() {
//    UITabBar.appearance().isTranslucent = false   // remove this line !!!
    UITabBar.appearance().barTintColor = UIColor(Color.pacificBlue)
}

替代:用配置的不透明背景替换整个外观

init() {

    let newAppearance = UITabBarAppearance()
    newAppearance.configureWithOpaqueBackground()
    newAppearance.backgroundColor = UIColor(Color.pacificBlue)
    UITabBar.appearance().standardAppearance = newAppearance

}

推荐阅读