[英]Swift error: Can't assign value of type UIColor to type CGColor
I have two input fields in my view, loginEmailInput
and loginPasswordInput
.我的视图中有两个输入字段,
loginEmailInput
和loginPasswordInput
。
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.