【问题标题】:SwiftUI: Gradient fill not showing upSwiftUI:渐变填充未显示
【发布时间】:2022-01-05 08:40:24
【问题描述】:

以下代码生成一个白色且未填充的Rectangle()。我对原因的想法为零,而且我没有收到任何错误。

注意:当我将自定义颜色(即Color(red: green: blue:) 替换为Color.redColor.blue 等内置颜色时,会显示填充并着色。

var body: some View {
        Rectangle()
            .fill(
                LinearGradient(
                    colors: [Color(red: 180, green: 40, blue: 55),
                             Color(red: 200, green: 40, blue: 55)],
                    startPoint: .top,
                    endPoint: .bottom)
            )
    }

是什么导致填充不显示,如何在保留自定义颜色的同时解决此问题?

【问题讨论】:

    标签: swift xcode swiftui linear-gradients


    【解决方案1】:

    您使用的 Color 初始化程序使用 sRGB 颜色空间定义颜色,并将任何提供的组件值限制在 0 和 1 之间。因此,为所有三个值提供大于 1 的任何值将创建白色 -相当于Color(red: 1.0, green: 1.0, blue: 1.0)

    要获得你需要的颜色,你应该将每个值除以 255,例如:

    colors: [Color(red: 0.706, green: 0.157, blue: 0.216),
             Color(red: 0.784, green: 0.157, blue: 0.216)],
    

    请参阅Apple documentation

    【讨论】:

    • 我一无所知...我忘记将值作为分数超过 255...谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 2011-08-22
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    相关资源
    最近更新 更多