0

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

ryla
  • 47
  • 1
  • 8

1 Answers1

1

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
}
frank
  • 71
  • 3
  • Thanks for answer Frank,Yes I know this way but my project supported iOS 11. Asset catalog is not supported iOS 11 – ryla Jun 26 '22 at 09:04
  • @yusufdemirkoparan Looks like iOS 13 introduced dark mode. So if you just update Xcode you should be able to see color asset. – Deepa Bhat Jun 27 '22 at 04:45
  • Yes I know that, but my project is supported by dark mode on iOS 11. We manually handle the theme changes. So we can't use any iOS 13 solutions – ryla Jun 27 '22 at 06:48