Skip to content
This repository was archived by the owner on Dec 4, 2025. It is now read-only.

Repository files navigation

ViewExtractor

Extract SwiftUI views from ViewBuilder content.

Installation

Follow the instructions at Adding Package Dependencies to Your App to find out how to install the Swift package. Use the link of this GitHub repo as the URL (https://github.com/GeorgeElsham/ViewExtractor).

Example #1

Create a vertical stack of views, separated by dividers.

View code

structDividedVStack<Content:View>:View{@ViewBuilderletcontent:Contentvarbody:someView{Extract(content){ views inVStack{letfirst= views.first?.id
ForEach(views){ view inif view.id != first {Divider()}
view
}}}}}

Usage

DividedVStack{Text("View 1")Text("View 2")Text("View 3")}

Result

Example #1 result

Example #2

Create a vertical stack of views, only keeping views at indexes with a multiple of 2.

View code

structIntervalVStack<Content:View>:View{@ViewBuilderletcontent:Contentvarbody:someView{Extract(content){ views inVStack{ForEach(Array(zip(views.indices, views)), id: \.1.id){ index, view inif index.isMultiple(of:2){
view
}}}}}}

Usage

IntervalVStack{ForEach(0..<10){ index inText("Index: \(index)")}}

Result

Example #2 result

Example #3

Create a stack of views, returning multiple views as a result, allowing it to be wrapped by another view. When modifiers are applied, they apply to each view returned. Note the use of ExtractMulti rather than just Extract.

View code

structDivided<Content:View>:View{@ViewBuilderletcontent:Contentvarbody:someView{ExtractMulti(content){ views inletfirst= views.first?.id
ForEach(views){ view inif view.id != first {Divider()}
view
}}}}

Usage

HStack{Divided{Text("View 1")Text("View 2")Text("View 3")}.border(Color.red)}

Result

Example #3 result

About

Extract SwiftUI views from ViewBuilder content. NOTE: You can now use `Groups.init(subviews:transform:)`/`ForEach.init(subviews:content:)` for iOS 18+.

Topics

Resources

Stars

122 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages