簡體   English   中英

SwiftUI 列表在 Sheet 中的外觀和行為與在 NavigationLink 中不同

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

我已經創建了一個列表視圖,其中包含用戶從(參見 LocationPickerModalView.swift)到 select 的部分,該列表可通過 NavigationLink 獲得並按預期工作。 然而,我決定將這個確切的視圖移動到工作表(參見 ThrillEditView.swift) ,但這極大地改變了視圖的外觀和工作方式。 (見下面的 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")
      }
    }
  }
}

行為示例

.sheet創建新的視圖層次結構,因此您需要在其中顯式NavigationView ,例如

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM