Steve
Steve

Reputation: 1153

Can't use variable to create custom swift color

I'm trying to use the variables red, green, and blue to create a color that is random, but I get an error that says it can't convert the variable type into a CGFloat.

import UIKit
var red = arc4random_uniform(8) + 1;
var green = arc4random_uniform(8) + 1;
var blue = arc4random_uniform(8) + 1;

let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 300.0, height: 600.0))

let circle = UIView(frame: CGRect(x: 105.0, y: 215.0, width: 100.0, height: 100.0))
circle.layer.cornerRadius = 50.0
let startingColor = UIColor(red: (253.0/255.0), green: (159.0/255.0), blue: (47.0/255.0), alpha: 1.0)
circle.backgroundColor = startingColor

containerView.addSubview(circle);

Upvotes: 0

Views: 69

Answers (1)

Encio Peter
Encio Peter

Reputation: 329

Try

let startingColor = UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: 1.0)

Upvotes: 1

Related Questions