Ben Ben - 1 year ago 389
iOS Question

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

I have two input fields in my view,


I'm trying to change the border color. My code in
looks like so:

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() {


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

How do I solve this issue?

Answer Source


UIColor.blueColor().CGColor  (example)

In general, layers/ CALayers use CG (Core Graphic) colors. While for something like background of the view, you can use something like view.backgroundColor = UIColor.blueColor() . UIColor is part of UIKit FrameWork (NSObject), CG is part of Core Graphic (CF/ Core Foundation Library)

For a layer, you do

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download