Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 334
Ben Avatar asked Jun 03 '16 18:06

Ben


1 Answers

For a CALayer, use, as an example:

UIColor.blue.cgColor 

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

Note: for very old versions of Swift (Swift 1 and 2), you would have use:

UIColor.blueColor().CGColor 
like image 157
Akshansh Thakur Avatar answered Sep 18 '22 14:09

Akshansh Thakur