I would like to inject my ViewModifier from a ViewModel which depends on user configuration.
Below code is a View which has a ViewModel. ViewModel can be created with ViewConfiguration and user should be able to either use a default style or custom style based on preferences.
When I try to do this it gives me error on View which says
Type ‘any ViewModifier’ cannot conform to ‘ViewModifier’
struct FeatureUnavailableView: View {
var viewModel = ViewModel(config: ViewConfiguration(titleStyle: DefaultTitleModifier()))
var body: some View {
Text("Hello World")
.modifier(viewModel.config.titleStyle)
}
}
class ViewModel {
let config: ViewConfiguration
init(config: ViewConfiguration) {
self.config = config
}
}
struct ViewConfiguration {
var titleStyle: any ViewModifier
}
public struct DefaultTitleModifier: ViewModifier {
public func body(content: Content) -> some View {
content
.lineLimit(1)
.foregroundColor(.red)
.bold()
}
}
2
Answers
Rather than using any ViewModifier you need to use generics to set the type as a concrete ViewModifier type when the ViewConfiguration struct and ViewModel class are initialised.
View
s don’t belong in Models, but you can use Models to make aView
dynamic.Instead of expecting
any ViewModifier
you can expectViewModiferModel
.Then store and transmit any values with that model.
The modifier itself would switch from hardcoded values to the values in the model.