I have a SwiftUI view with a few text fields at the top, and I am trying to dynamically change the color of these. The best option I have found so far is to keep a @State
variable that holds an array of colors corresponding to the colors of each of the text fields. However, I am trying to update this @State
from another class, which doesn't seem possible.
To try to fix this, I was looking at using an ObservableObject
with a @Published
array that works the same way as the previously mentioned array of colors: the class that would do the changing of colors would have this array within it somewhere, and would update the array with new colors; the View
would then have an @ObservedObject
, and each Text
would reference the @ObservedObject
.
Here is my issue though: the function that I want to be able to change the colors is static. Swift will not let me make a @Published static var
, so this static function can't change the array that is the focus of the @ObservedObject
. Is there another way to set up something similar to @State
or @ObservedObject
that would allow me to dynamically update colors, that is also accessible from a static function?
Here is a rough idea of what I am hoping to set up:
The view that holds the Text elements:
struct TextView: View {
@ObservedObject var colorChange = ColorChangingClass()
var body: some View {
HStack {
Text("Text Field 1")
.padding()
.background(Color.gray)
.foregroundColor(colorChange.colorArray[0])
.cornerRadius(5.0)
Text("Text Field 2")
.padding()
.background(Color.gray)
.foregroundColor(colorChange.colorArray[1])
.cornerRadius(5.0)
Text("Text Field 3")
.padding()
.background(Color.gray)
.foregroundColor(colorChange.colorArray[2])
.cornerRadius(5.0)
}
}
}
The class with the static function that alters the colors:
class ColorChangingClass: ObservableObject {
static func changingColors() {
// I want to be able to change the colors from here
}
// Swift won't let me make this static
@Published var colorArray: [Color] = [Color.blue, Color.red, Color.green]
}
The placement of the colorArray is flexible so long as I can access it from this static function, this just seemed to be the closest thing to a solution that I could find, but obviously it doesn't quite work.
CodePudding user response:
Dynamically changing colors with @State or @ObservableObject using static functions in Swift
Simple answer: this is not possible.
Simple reason: @Published
works only on instance var and not static
As there is missing context I just asume you want to change the colors from different views. If thats the reason for implementing the function as static there is a workaround.
Instantiate your ColorChangingClass
at the top most View that uses it as a @StateObject
. Hand it down to the child views via .environmentObject()
.
If you provide more context there could be other possible solutions too.