Reputation: 129
I want change the text background color with passing function to the .background()like this, the value of color is 0 or 1 or 2 will depend on the data of DB .how can I fix it:
import SwiftUI
struct ContentView: View {
@State var color = 0;
var body: some View {
Text("Hello, World!")
.background(changeBkColor(int : self.$color))
}
}
func changeBkColor(int : color)
{
if(color == 1)
{
return Color.red;
}
else if(color == 2)
{
return Color.green;
}
else
{
return Color.blue;
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Upvotes: 7
Views: 6588
Reputation: 257493
Here it is
struct ContentView: View {
@State var color = 0;
var body: some View {
Text("Hello, World!")
.background(changeBkColor(color: self.color))
}
}
func changeBkColor(color: Int) -> Color
{
if(color == 1)
{
return Color.red;
}
else if(color == 2)
{
return Color.green;
}
else
{
return Color.blue;
}
}
Upvotes: 5