You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi!
I need to pass down custom environment values to my variadic children. I've noticed that this is impossible, see a minimal reproduction below:
import SwiftUI
import VariadicViews
structTestKey:EnvironmentKey{staticvardefaultValue:Int=0}extensionEnvironmentValues{vartestValue:Int{get{self[TestKey.self]}set{self[TestKey.self]= newValue }}}structVariadicThing<Content:View>:View{@ViewBuilderletcontent:Contentvarbody:someView{MultiVariadicView(content){ children inForEach(children){ child in
child
.environment(\.testValue,42) // my custom environment value is not honored
.environment(\.colorScheme,.dark) // but any system-provided env. value is honored!
}}}}structTester:View{letname:String@Environment(\.testValue)vartestValuevarbody:someView{Text("\(name) = \(testValue)").background(Color(uiColor:.systemBackground))}}
#Preview {VStack{VariadicThing{Tester(name:"Test 1")Tester(name:"Test 2")}Text("All should be = 42").foregroundStyle(.secondary)}.environment(\.colorScheme,.light)}
Here is the result:
It should read Test1 = 42 and Test2 = 42. The system-provided environment (\.colorScheme) is correctly passed down to each child, but not my custom environment value.
It works if I do
MultiVariadicView(content.environment(\.testValue,42)){ children in...}
but that's not what I want: in my actual project, the values can differ for each child, so I need it inside the ForEach.
Is this a limitation from using a private-ish API like _VariadicView ?
I know this library might very well not be maintained for much longer because iOS 18 offers this natively, but my project targets iOS 16.
The text was updated successfully, but these errors were encountered:
After further research, the problem also arises with the new Group(subviewsOf:transform:) in iOS 18 (which coincidentally has the same API than this package).
This indeed seems like a bug in SwiftUI. I've filed a radar under #FB14070467 which you can feel free to duplicate.
Here's the attached demo project I sent to Apple. It both exhibits the problem with the new iOS 18 API, and with this package. As-is, it needs Xcode 16 to compile, but it also runs on Xcode 15 and iOS 16 when commenting out the few lines related to the new API.
Hi!
I need to pass down custom environment values to my variadic children. I've noticed that this is impossible, see a minimal reproduction below:
Here is the result:

It should read
Test1 = 42
andTest2 = 42
. The system-provided environment (\.colorScheme
) is correctly passed down to each child, but not my custom environment value.It works if I do
but that's not what I want: in my actual project, the values can differ for each child, so I need it inside the
ForEach
.Is this a limitation from using a private-ish API like
_VariadicView
?I know this library might very well not be maintained for much longer because iOS 18 offers this natively, but my project targets iOS 16.
The text was updated successfully, but these errors were encountered: