Tel Tel - 7 months ago 11
Swift Question

How to change programmatically the background color of in tvOS in Swift?

I would like to change programmatically the color of the background in the main view (I'm creating an app with Swift).I tryed to put this code in viewDidLoad() but it doesn't work. What is the correct way to do this?

override func viewDidLoad() {
super.viewDidLoad()
...
let myBackground = UIView()
let colorWhite = UIColor(red: 255, green: 255, blue: 255, alpha: 1)
myBackground.backgroundColor = colorWhite
...
}
}

Answer

You are creating a new subview, which is not added to your view stack yet. Instead, it's much simpler:

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.whiteColor()

    // Or with RGB:
    view.backgroundColor = UIColor(red: 142 / 255.0, green: 142 / 255.0, blue: 147 / 255.0, alpha: 1.0)
}