Ben Ben - 6 months ago 97
iOS Question

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

I have two input fields in my view,

loginEmailInput
and
loginPasswordInput
.

I'm trying to change the border color. My code in
ViewController.swift
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() {
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


How do I solve this issue?

Answer

Use

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

UIColor.blueColor().CGColor
Comments