小编che*_*843的帖子

如何在swiftui中动态更改文本背景颜色?

我想通过将函数传递给 .background() 来更改文本背景颜色,如下所示,颜色的值为 0 或 1 或 2 将取决于数据库的数据。我该如何修复它:

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()
    }
}
Run Code Online (Sandbox Code Playgroud)

ios swift swiftui

7
推荐指数
1
解决办法
5904
查看次数

标签 统计

ios ×1

swift ×1

swiftui ×1