I'm very curios how .swipeActions of List view implemented
This is how .swipeActions looks like
List {
Text("Test")
.swipeActions {
Button("Action1") {}
Button("Action2") {}
}
}
.swipeActions accepts ViewBuilder as a content
The interesting part is If I add anything else then Button, it will be ignored.
List {
Text("Test")
.swipeActions {
Button("Action1") {}
Text("Test")
Button("Action2") {}
}
}
Text is ignored here !
How to achieve this ?
I want to make custom View with similar functionality.
How to create ViewModifier that takes ViewBuilder as parameter and then filter everything except Button ?
The
Texts are technically not "filtered out" byswipeActions. As far asswipeActionsis concerned, theTexts exist.It is when the "backend" of SwiftUI (whatever is creating the actual
UICollectionViewcells from yourViewstructs) sees these views, that theTexts get discarded. That code doesn't know how to deal withTexts.A similar thing happens in
Menu,.tabItem, etc. These only support a subset of SwiftUI views, but it still compiles if you write unsupported views in their view builders.Obviously, you don't have access to the "backend" of SwiftUI, so you cannot do the same.
You can, however, disallow non-
Buttons at compile time, by writing your own version of@ViewBuilderthat only buildsButtons:and have your modifier take a
@ButtonsBuilderinstead of@ViewBuilder. But unlike a@ViewBuilder, you cannot useif/switchand other control flow structures.