0

Currently trying to determine if there is a way to programmatically set colors in SwiftUI.

Here's an example of the code I have that doesn't work.

ForEach(listItems, id: \.self) { item in
                                Text(item.name).listRowBackground(Color(item.color))
                            }

However manually specifying the color like so does.

ForEach(listItems, id: \.self) { item in
                                Text(item.name).listRowBackground(Color.red)
                            }

Just wanting to know if anyone else has determined a way dynamically set this with a large selection of colors.

Thanks

bmonsalvatge
  • 101
  • 1
  • 5
  • What do you mean here by "doesn't work?" Do you receive an error? What is `item.color`? Do you have a self-contained example that demonstrates this issue? – Rob Napier Feb 03 '20 at 03:01
  • It does work... debug how you create `Color(item.color)` and if it is really created. – Asperi Feb 03 '20 at 05:45
  • Sorry, to clarify `item.color` contains a string with the name of a variable, such as `red`, or `blue`. To add, I don't receive an error, but nothing happens in the UI. – bmonsalvatge Feb 04 '20 at 01:10
  • Realizing that passing it as a string is not correct. Referenced this post as how to use it properly: https://stackoverflow.com/a/57263995/2520506. `item.color` is not set to type UIColor. variable is set to the following: `UIColor.systemRed`. Thanks again for the help. – bmonsalvatge Feb 04 '20 at 01:56

0 Answers0