I want to use my theme color on all instances of Toggle()
, is there a way to do this with an extension?
extension Toggle {
func content() -> some View {
self.tint(.red)
}
}
The above is not working, is there something else I should call on the extension to modify all instances of Toggle
?
3
Answers
The best way to do this is to make a custom view with
@ViewBuilder
.If you want to create a modifier to apply to an instance of
Toggle()
, can do that with the help ofViewModifiers
.i.e: First create a ViewModifier:
Now you can use the extension this way:
This is exactly what
.toggleStyle
is designed for. Create your own custom ToggleStyle:Then in your top-level ContentView, add the modifier:
Your style will be applied to all Toggles inside of your ContentView.