5

自 iOS 14 以来,我有一个奇怪的行为,我在 NavigationView 中的列表周围有一个填充,只要我添加了 navigationBarItems ...

我的代码:

import SwiftUI

struct TestList: View {
  var body: some View {
    NavigationView{
      List {
        Text("hello world")
        Text("hello world")
        Text("hello world")
      }
      .navigationBarTitle(Text("Test List"), displayMode:.inline)
      .navigationBarItems(leading:
                            Image(systemName: "bell")
      )
    }
  }
}

struct TestList_Previews: PreviewProvider {
  static var previews: some View {
    TestList()
  }
}

我怎样才能解决这个问题?

谢谢

4

1 回答 1

12

好,我知道了...

我需要将 ListStyle 添加到列表中:

https://developer.apple.com/documentation/swiftui/liststyle

import SwiftUI

struct TestList: View {
  var body: some View {
    NavigationView{
      List {
        Text("hello world")
        Text("hello world")
        Text("hello world")
      }
      .listStyle(PlainListStyle())
      .navigationBarTitle(Text("Test List"), displayMode:.inline)
      .navigationBarItems(leading:
                            Image(systemName: "bell")
      )
    }
  }
}

struct TestList_Previews: PreviewProvider {
  static var previews: some View {
    TestList()
  }
}
于 2020-10-11T15:00:50.557 回答