🗒️

[SwiftUI] viewのcollectionに対してseparatorをjoinするview

2024/10/14に公開
struct SplittingView<Content: View, Splitter: View>: View {
  
  let content: Content  
  let splitter: Splitter  
  
  init(
    @ViewBuilder content: () -> Content,
    @ViewBuilder splitter: () -> Splitter
  ) {
    self.content = content()
    self.splitter = splitter()
  }
  
  var body: some View {
    Group(subviews: content) { collection in
      let lastID = collection.last?.id
      ForEach(subviews: collection) { subviews in
        subviews
        if subviews.id != lastID {
          splitter
        }
      }   
    }
  }
}

Discussion