【问题标题】:Color code from different class doesn't show propper color in main class Swift?来自不同类的颜色代码在主类 Swift 中不显示正确的颜色?
【发布时间】:2021-02-25 12:28:41
【问题描述】:

我试图从一个单独的类中提取颜色到我的视图中,但是当我这样调用它时,颜色只显示 Black 而不是 orange。这怎么可能?

struct Small: View {
    
    var styling: ViewStyling?

    var body: some View {
        VStack {
                Image(systemName: "circle.fill")
                    .resizable()
                    .frame(width: 10, height:10)
                    .foregroundColor(styling?.orangeDot) <--- Color should be Orange
                    .padding(.trailing, -4)
               }
          }
     }

颜色类看起来像这样。

class ViewStyling {
    
    var orangeDot:Color = Color(#colorLiteral(red: 0.9333333333, green: 0.6784313725, blue: 0.262745098, alpha: 1))
    
}

【问题讨论】:

  • styling?.orangeDot 为零吗?是stylingnil?设置好了吗?
  • 你如何在Small 上设置styling?您需要edit 您的问题以minimal reproducible example 的形式包含所有相关代码,以便使问题成为主题。

标签: swift image colors swiftui


【解决方案1】:

你还没有创建 ViewStyling 对象,所以它返回 nil。 首先,创建 ViewStyling 对象。

struct Small: View {
    
    var styling: ViewStyling = ViewStyling() //<-Here
    
    var body: some View {
        
        VStack {
            Image(systemName: "circle.fill")
                .resizable()
                .frame(width: 10, height:10)
                .foregroundColor(styling.orangeDot)// <--- Color should be Orange
                .padding(.trailing, -4)
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2016-10-10
    • 1970-01-01
    • 2012-01-19
    相关资源
    最近更新 更多