I have a custom view:
public struct CustomView: View {
var text1: String
var text2: String
public var body: some View {
VStack {
Text(text1).tag(0)
Text(text2).tag(1)
}
}
}
I want to create view modifier and pass custom Style parameter to it:
struct Watermark: ViewModifier {
var style: Style
func body(content: Content) -> some View {
if style == .one {
// access Text with tag 0 and set foreground color to red
// access Text with tag 1 and set foreground color to blue
} else if style == .two {
// access Text with tag 0 and set foreground color to black
// access Text with tag 1 and set foreground color to pink
}
}
How to access content child view from Modifier to change it style ?
I would like to do something similar as apple do with SF Image
P.S.: I want to use exactly view modifier. I don't want to pass Style as parameter to view. Because I want to build complex custom views where it can be more then 7 parameters. And I don't want to pass 7 parameters to view initialiser. View modifiers looks more nicer.


You should not use
tagfor identifying views.tagis very hard to read. The view trait key for it is an internal type, so you'd need to useMirror.You can use the environment instead. Create an environment key for each of the 7+ parameters you will have.
In this case, there are 2 colors, so I'll create 2 keys. If there are more colors, I'd instead use one key that has an array as a value.
Instead of
tag, write your own view modifiers that read from the environment.Then
Watermarkcan write to the environment:Here is an implementation of the same idea but with one environment key holding an array of color styles: