简体   繁体   English

如何删除 SwiftUI 列表顶部的空格

[英]How to remove blank space on top of List in SwiftUI

SwiftUI reserves blank space on top of List for the title even though there is no title used.即使没有使用标题,SwiftUI 也会在列表顶部为标题保留空白空间。

How do i remove this space?我如何删除这个空间?

.navigationBarHidden() is not an option because the toolbar icons need to be shown. .navigationBarHidden() 不是一个选项,因为需要显示工具栏图标。

在此处输入图像描述

That space is associated with the NavigationView that your List is in - each view within the "stack" of the navigation's displayable views can set its own title that would then get displayed in that spot.该空间与您的列表所在的 NavigationView 相关联 - 导航可显示视图的“堆栈”中的每个视图都可以设置自己的标题,然后将显示在该位置。

If you're going to keep that space unused, you can mitigate by forcing the navigation title to be in inline mode.如果您要保持该空间未使用,您可以通过强制导航标题处于内联模式来缓解。 This is what you see when you scroll down a list and the big title shrinks and moves up into the toolbar space, between the leading and trailing toolbar items.当您向下滚动列表并且大标题缩小并向上移动到工具栏空间中,在前导和尾随工具栏项之间,这是您所看到的。

To do this, add the .navigationBarDisplayMode modifier to your list:为此,请将.navigationBarDisplayMode修饰符添加到您的列表中:

  // within a NavigationView context
  List {
     // ...
  }
  .navigationBarTitleDisplayMode(.inline)
List {
    Text("Space1 ")
}
.listStyle(PlainListStyle())

try this...尝试这个...

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

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