Even when you create a fresh single view project (Xcode 9.2), create a single named color in an asset catalog, set the background color of the main view controller to this color in IB, and try to update the background color in viewDidLoad, the background color won't change. Have other people experienced this? Is this normal behavior?
view.backgroundColor = .red // Doesn't work, the asset color set in IB remains
Super interesting and you are correct it does not change the color value as expected. There must be some delay as to when IB attributes are set in the super class. Wrapping the color change in DispatchQueue.main.async makes it work in viewDidLoad. Probably because this code would be processed last after whatever the super class is doing with IB attributes. Hope this solves the issue.
DispatchQueue.main.async {
self.view.backgroundColor = UIColor.blue
}
Also in viewDidLayoutSubviews and viewDidAppear it works without the DispatchQueue but has the same behavior as viewDidLoad in viewWillAppear of not working as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With