【问题标题】:Cannot assign value of type 'Color' to type 'UIColor'无法将“颜色”类型的值分配给“UIColor”类型
【发布时间】:2020-07-21 20:08:43
【问题描述】:

当我在 UITextViewWrapper 类中设置 textField.textColor = Color.primary 时,会出现此错误。

如何在 UITextViewWrapper 中使用原色?有什么方法可以使用吗?

fileprivate struct UITextViewWrapper: UIViewRepresentable {
    typealias UIViewType = UITextView

    func makeUIView(context: UIViewRepresentableContext<UITextViewWrapper>) -> UITextView {
        let textField = UITextView()
        textField.textColor = Color.primary // here give error
        return textField
    }
          
 }

【问题讨论】:

标签: ios swift swiftui


【解决方案1】:

在 iOS 14+ 上,您可以使用新的 UIColor 初始化程序。

textField.textColor = UIColor(Color.primary)

对于较低版本,您可以使用:

textField.textColor = UIColor.label // gives the same result

【讨论】:

    【解决方案2】:
    1. 扩展 UIColor:

      extension UIColor {
          class var primary: UIColor {
              return UIColor(named: "YourColorName")!
          }
      }
      
    2. 用法:

      textField.textColor = .primary
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 2021-04-30
      • 2019-12-03
      • 2017-08-07
      相关资源
      最近更新 更多