线性颜色渐变到HStack中的项目?

时间:2020-05-01 12:14:32

标签: ios swiftui linear-gradients hstack

SwiftUI中是否可以为HStack中的所有项目添加渐变颜色。

enter image description here

您可以将单独的颜色同时应用于HStack .background.foregroundColor,但是由于LinearGradient是符合View的结构,因此无法将其传递给.foregroundColor,因为它期望颜色。

您可以通过多种方式解决此问题(下面是一个使用不透明度的示例),但是如果我遗漏了其他东西,我对这么多SwiftUI选项感到好奇吗?

SwiftUI示例:

struct GradView: View {
    var body: some View {
        HStack {
            ForEach((1...5).reversed(), id: \.self) { index in
                RoundedRectangle(cornerRadius: 5)
                    .frame(width: 50, height: 50)
                    .opacity(Double(index) / 5)
            }
        }.foregroundColor(Color.red)
    }
}

SwiftUI输出:

enter image description here

1 个答案:

答案 0 :(得分:1)

有内置的Gradient ...如何呈现它取决于我们...这里可能是替代方案,但这只是...许多其他变体之一。.

demo

<OverlayModal open={true} ><p>content goes there</p></OverlayModal >