TFLOW9Z1 TFLOW9Z1 - 1 month ago 6
Swift Question

UIColor.greenColor() andUIColor.blueColor() is giving me error in xcode

let firstFrame = CGRect(x: 160, y: 240, width: 100, height: 150)
let firstView = UIView(frame: firstFrame)
firstView.backgroundColor = UIColor.blueColor()
view.addSubview(firstView)

let secondFrame = CGRect(x: 20, y: 30, width: 50, height: 50)
let secondView = UIView(frame: secondFrame)
secondView.backgroundColor = UIColor.greenColor()
view.addSubview(secondView)


This is the question the code above I'm having problem with changing the background color for view in swift3. The error that I get in the xcode compiler is, "cannot call value of non-function type 'UIColor'". Can anyone help me figure a solution to this problem. Is There a function that grabs a color for backgroundColor?

Answer

If you working in Swift 3 it should be as below:

let firstFrame = CGRect(x: 160, y: 240, width: 100, height: 150)
let firstView = UIView(frame: firstFrame)
firstView.backgroundColor = UIColor.blue
view.addSubview(firstView)

let secondFrame = CGRect(x: 20, y: 30, width: 50, height: 50)
let secondView = UIView(frame: secondFrame)
secondView.backgroundColor = UIColor.green
view.addSubview(secondView)