简体   繁体   English

SwiftUI iOS14 - NavigationView + List - 不会填充空间

[英]SwiftUI iOS14 - NavigationView + List - Won't fill space

I'm having an issues with a List inside a NavigationView since iOS 14 update.自 iOS 14 更新以来,我遇到了 NavigationView 中的 List 问题。

Here is a simple breakdown of the code - I've striped everything that doesn't show the issue这是代码的简单细分 - 我已经将所有未显示问题的内容都进行了条带化

struct ContentView: View {
    
    var views = ["Line 1", "Line 2", "Line 3"]
    
    var body: some View {
        
        NavigationView {
            
            VStack {
                
                List {
                    
                    ForEach(views, id: \.self) { view in
                       
                        VStack {
                        Text("\(view)")
                        }
                        .background(Color.red)
                        
                    }
                    
                }
                
            }
            
        }
        
    }
}

This produces the following result:这会产生以下结果:

导航视图中的列表

I cant work out why the list is hovering in the center of the navigation view like that.我无法弄清楚为什么列表会像那样悬停在导航视图的中心。 As far as I can tell this should produce a listview that takes up all avaliable space (with the exception of the top where navigationbar would be).据我所知,这应该会生成一个占据所有可用空间的列表视图(导航栏所在的顶部除外)。

Indeed when run on iOS 13.5 that is the result I get as pictured below:事实上,当在 iOS 13.5 上运行时,我得到的结果如下图所示:

在导航视图 2 中列出

I've had a read through the documentation but cant work out why this behaviour is suddenly happening.我已经通读了文档,但无法弄清楚为什么会突然发生这种行为。

Any help would be greatly appreciated.任何帮助将不胜感激。

Thanks谢谢

Problem问题

It looks like the default styles of a List or NavigationView in iOS 14 may in some cases be different than in iOS 13.看起来 iOS 14 中ListNavigationView的默认样式在某些情况下可能与 iOS 13 中的不同。

Solution #1 - explicit listStyle解决方案 #1 - 显式listStyle

It's no longer always the PlainListStyle (as in iOS 13) but sometimes the InsetGroupedListStyle as well.它不再总是PlainListStyle (如在 iOS 13 中),但有时也是InsetGroupedListStyle

You need to explicitly specify the listStyle to PlainListStyle :您需要将listStyle明确指定为PlainListStyle

.listStyle(PlainListStyle())

Example:例子:

struct ContentView: View {
    var views = ["Line 1", "Line 2", "Line 3"]

    var body: some View {
        NavigationView {
            VStack {
                List {
                    ForEach(views, id: \.self) { view in
                        VStack {
                            Text("\(view)")
                        }
                        .background(Color.red)
                    }
                }
                .listStyle(PlainListStyle()) // <- add here
            }
        }
    }
}

Solution #2 - explicit navigationViewStyle解决方案#2 - 显式导航视图样式

It looks like the NavigationView's default style can sometimes be the DoubleColumnNavigationViewStyle (even on iPhones).看起来 NavigationView 的默认样式有时可以是DoubleColumnNavigationViewStyle (即使在 iPhone 上)。

You can try setting the navigationViewStyle to the StackNavigationViewStyle (as in iOS 13):您可以尝试将navigationViewStyle设置为StackNavigationViewStyle (如在 iOS 13 中):

.navigationViewStyle(StackNavigationViewStyle())

Example:例子:

struct ContentView: View {
    var views = ["Line 1", "Line 2", "Line 3"]

    var body: some View {
        NavigationView {
            VStack {
                List {
                    ForEach(views, id: \.self) { view in
                        VStack {
                            Text("\(view)")
                        }
                        .background(Color.red)
                    }
                }
            }
        }
        .navigationViewStyle(StackNavigationViewStyle()) // <- add here
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM