【问题标题】:Fill a RoundedRectangle with custom color用自定义颜色填充 RoundedRectangle
【发布时间】:2019-10-14 11:26:56
【问题描述】:
 RoundedRectangle(cornerRadius: 8, style: .continuous)
    .foregroundColor(Color.init(red: 255, green: 245, blue: 158))

我的圆角矩形全是白色的。为什么我不能启动自定义颜色?而 Color.red 工作得非常好?

【问题讨论】:

    标签: watchkit swiftui


    【解决方案1】:
    Color.init(red: CGFloat, green: CGFloat, blue: CGFloat)
    

    采用 3 CGFloats,其值介于 0 和 1 之间

    你需要的是……

    Color(red: 255/255, green: 245/255, blue: 158/255)
    

    请注意,.init 在 Swift 中不是必需的

    【讨论】:

      【解决方案2】:

      您可以编写一个扩展以将Ints 作为参数传递给Color 初始化程序。它将允许您使用Color(red: 255, green: 245, blue: 158):

      extension Color {
      
          public init(red: Int, green: Int, blue: Int) {
              self = Color(red: Double(red)/255.0,
                           green: Double(green)/255.0,
                           blue: Double(blue)/255.0)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-15
        • 1970-01-01
        • 2011-06-05
        • 1970-01-01
        相关资源
        最近更新 更多