简体   繁体   English

SwiftUI 列表在 Sheet 中的外观和行为与在 NavigationLink 中不同

[英]SwiftUI List looks and behaves differently in Sheet than in NavigationLink

I've created a list view with sections for the user to select from (See LocationPickerModalView.swift) , this list is available via a NavigationLink and works as expected.我已经创建了一个列表视图,其中包含用户从(参见 LocationPickerModalView.swift)到 select 的部分,该列表可通过 NavigationLink 获得并按预期工作。 However I decided to move this exact view to a Sheet (See ThrillEditView.swift) , but this drastically changed how the view looks and worked.然而,我决定将这个确切的视图移动到工作表(参见 ThrillEditView.swift) ,但这极大地改变了视图的外观和工作方式。 (See Gif below) Would there be a way to get the same behaviour using a Sheet? (见下面的 Gif)有没有办法使用工作表获得相同的行为?

// ThrillEditView.swift
public struct ThrillEditView: View {
  @State private var isLocationPickerSheetPresented: Bool = false

  public var body: some View {
    form {
      // ...
      NavigationLink("NavigationLink", destination: LocationPickerModalView())
      Button("Sheet (Via Button)") {
        self.isLocationPickerSheetPresented = true
      }
      // ...
    }
    .sheet(isPresented: self.$isLocationPickerSheetPresented, content: {
      LocationPickerModalView()
    })
  }
}
// LocationPickerModalView.swift
public struct LocationPickerModalView: View {
  public var body: some View {
    List {
      Button("1")
      Button("2")
      Section(header: Text("one")) {
        Button("3")
        Button("4")
      }
      Section(header: Text("two")) {
        Button("5")
        Button("6")
      }
    }
  }
}

行为示例

The .sheet creates new view hierarchy, so you need explicit NavigationView in it, like .sheet创建新的视图层次结构,因此您需要在其中显式NavigationView ,例如

.sheet(isPresented: self.$isLocationPickerSheetPresented, content: {
  NavigationView {
     LocationPickerModalView()
  }
})

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

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