Reputation: 79
I am trying to change my background color for my view programmatically because later on I will have to switch the color based on user input. I have no idea why this wont change the background color.
class MainViewController: UIViewController {
@IBOutlet weak var rLabel: UILabel!
@IBOutlet weak var gLabel: UILabel!
@IBOutlet weak var bLabel: UILabel!
@IBOutlet weak var largeColorView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
func colorWithAlphaComponent(alpha: CGFloat) -> UIColor{
let newColor = UIColor.init(red: 0.5, green: 0.8, blue: 1.0, alpha: alpha)
return newColor
}
print(largeColorView.backgroundColor)
print(colorWithAlphaComponent(1.0))
largeColorView.backgroundColor! = colorWithAlphaComponent(1.0)
print(largeColorView.backgroundColor)
// Do any additional setup after loading the view.
}
Upvotes: 0
Views: 1107
Reputation: 3030
First mistake : you have to implement the function outside of the the viewDidLoad() method but you call of it. Therefore your code should like this :
override func viewDidLoad() {
super.viewDidLoad()
print(largeColorView.backgroundColor)
print(colorWithAlphaComponent(1.0))
largeColorView.backgroundColor! = colorWithAlphaComponent(1.0)
print(largeColorView.backgroundColor)
}
func colorWithAlphaComponent(alpha: CGFloat) -> UIColor{
let newColor = UIColor.init(red: 0.5, green: 0.8, blue: 1.0, alpha: alpha)
return newColor
}
Therefore this line of code will run because it call the function
largeColorView.backgroundColor! = colorWithAlphaComponent(1.0)
Upvotes: 1