Swift Dark mode supported on a presenter model

91 views Asked by At

I have a struct UI model. I create this model on ViewModel. I put an example below.

TextRow(title: "Come", font: .myFont(.bold, size: 14), color: .appBlack, alignment: .center)

I use that model in a table view cell. When I change the theme, .appBlack is not updated. I tried table view reloading data but it doesn't work. How can I update .appBlack? I want different colors light and dark modes. I read one more article but I don't find any solution to that issue.

I've tried this method but it doesn't update it because I've created my model in ViewModel.

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {}

How can I fix this issue?

Thanks for help

1

There are 1 answers

3
frank On

You can use an asset catalog to define a dynamic color:

Choose "Any, Dark" from appearances within the attribute inspector to get an additional color for dark appearance: enter image description here

Alternatively you can also define a dynamic color programmatically by using UIColor's init(dynamicProvider:) as follows:

let dynamicColor = UIColor { traitCollection in
    traitCollection.userInterfaceStyle == .dark ? .red : .green
}