简体   繁体   English

Swift 错误:无法将类型 UIColor 的值分配给类型 CGColor

[英]Swift error: Can't assign value of type UIColor to type CGColor

I have two input fields in my view, loginEmailInput and loginPasswordInput .我的视图中有两个输入字段, loginEmailInputloginPasswordInput

I'm trying to change the border color.我正在尝试更改边框颜色。 My code in ViewController.swift looks like so:我在ViewController.swift中的代码如下所示:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var loginPasswordInput: UITextField!
    @IBOutlet weak var loginEmailInput: UITextField!

    let borderColor : UIColor = UIColor(red:0.39, green:0.76, blue:0.37, alpha:1)
    loginEmailInput.layer.borderColor = borderColor
    loginPasswordInput.layer.borderColor = borderColor

    override func viewDidLoad() {
        super.viewDidLoad()

        //etc....

However, both these lines show an error:但是,这两行都显示错误:

loginEmailInput.layer.borderColor = borderColor
loginPasswordInput.layer.borderColor = borderColor

The error is:错误是:

Can't assign value of type UIColor to type CGColor无法将类型 UIColor 的值分配给类型 CGColor

How do I solve this issue?我该如何解决这个问题?

For a CALayer, use, as an example:对于 CALayer,例如使用:

UIColor.blue.cgColor

In general, layers/ CALayers use CG (Core Graphics) colors.一般来说,layers/ CALayers使用 CG(Core Graphics)颜色。 While for something like background of the view, you can use something like view.backgroundColor = .blue .对于视图背景之类的东西,您可以使用view.backgroundColor = .blue类的东西。 UIColor is part of UIKit FrameWork (NSObject), CG is part of Core Graphic (CF/ Core Foundation Library) UIColor 是 UIKit FrameWork (NSObject) 的一部分,CG 是 Core Graphic (CF/Core Foundation Library) 的一部分

Note: for very old versions of Swift (Swift 1 and 2), you would have use:注意:对于非常旧的 Swift 版本(Swift 1 和 2),您可以使用:

UIColor.blueColor().CGColor
imageView.layer.borderColor = UIColor.black.cgColor

We can define the color by converting UIcolor to ciColor.我们可以通过将 UIcolor 转换为 ciColor 来定义颜色。 With the code below:使用以下代码:

imageView.layer.borderColor = UIColor(ciColor: .blue)

in my Case: using Custom Color:在我的案例中:使用自定义颜色:

   view.layer.borderColor = Colors.Mountain.color.cgColor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型“ UIColor”的值分配为类型“ String?” - Cannot assign value of type 'UIColor' to type 'String?' 无法将“颜色”类型的值分配给“UIColor”类型 - Cannot assign value of type 'Color' to type 'UIColor' 错误:“无法将 String: UIColor 类型的值分配给 String: AnyObject 类型的值!” - Error: "Cannot assign a value of type String: UIColor? to a value of type String: AnyObject!" swift 3 中的 UIColor.redColor().CGColor - UIColor.redColor().CGColor in swift 3 无法分配类型为“ NSObject”的值? 到“ UIColor”类型的值? - Cannot assign a value of type 'NSObject?' to a value of type 'UIColor?' 错误:无法将类型“ [SomeObject]”的值分配给类型“ [NSMutableArray]” - Error: Can't assign Value of type '[SomeObject]' to type '[NSMutableArray]' Swift:无法为“ int”类型的值分配“ int”类型的值? 错误 - Swift: Cannot assign a value of type 'int' to a value of type 'int?' Error swift无法将类型“ UITableViewCell”的值分配为类型“…Cell”的错误 - Cannot assign value of type 'UITableViewCell' to type '…Cell' error in swift 错误:无法将类型“[String]”的值分配给 swift 中的“String”类型 - Error: Cannot assign value of type '[String]' to type 'String' in swift 无法将类型“ Any”的值分配为类型“ String”?Swift 3(iOS) - Can not assign a value of type “Any” to type “String?” Swift 3 (iOS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM