Background color not set in iOS 12

  • I created a view in IB

  • I set it's background color to color A

  • I then set it's background color to color B, in viewDidLoad()

  • In iOS 13 simulators, the view shows as expected with color B

  • In iOS 12 simulators, the view shows with color A, which I don't expect, nor want

(note: it appears that using didLayoutSubviews works, but why should iOS 12 and 13 behave differently? Either both should work with viewDidLoad, or neither should)
Hello! In order to help you better, could you post the code you used in order to set the background color? Thank you!
Yeah, it should defitnitly behave same on iOS 12. I would assume that the simulator is bugging since I had similar cases before.
The reason I didn't post it to begin with is I doubt it matters, as it's straight up the one way to do it, but sure:

Code Block
override func viewDidLoad() {
    super.viewDidLoad()
myView.backgroundColor = UIColor(named: myColorB)
}

Line 3 overrides myColorA which is set in IB, but only in iOS 13, not iOS 12.
For iOS 12 this code must be in viewWillLayoutSubviews() (or later, I guess would work as well)
Actually this issue occurs on physical devices as well
Have you tried using a sample UIColor like UIColor.black?
The only thing that comes to my mind is that iOS 12 doesn't handle dark mode and if your color is on the .xcassets with dark mode set maybe it gives it some problems

  • Both the colors in IB and the one I set via code are custom colors but are universal - no dark mode value.

  • Also, if the issue is with iOS 12 not supporting dark mode, why does moving it to layoutSubviews fix it?

  • Testing with setting IB to custom color and via code to a regular color in viewDidLoad: same issue

  • Testing with setting IB to a regular color, and then my custom color in code: fixes it!

I still think this is a bug to some degree, because my colors don't have dark mode values, but this is definitely good to know! Thank you

I hope someone from Apple will chip in...
Faced with this issue recently

I had xib file for table cell and custom colors in xcasset.
Colors are single appearance (dark mode not supported).
In cell swift class I have bool variable with didSet, where few outlets are modified: view.backgroundColor and label.textColor. Their values are based on variable value. There are ternar conditions so I was sure that color will be selected correctly.

On devices with iOS 12 (checked on 12.1 and 12.4 real and simulators) color didn't change at start but only after cell reuse.

After finding this question and few experiments, I have found that:
Setting custom color in xib file (no matter, if they have dark version or not) was performed after my didSet block and overrides my conditions results. And since I have set one of possible color in xib, I though that problem in data.

So I have reset outlets colors to default in xib and now it works
In case if you have to display some default color before some conditions, I guess putting it in init methods (awakeFromNib, viewDidLoad etc) should work

This bug was fixed in 13.0+

 

Sorry to dig up an old thread, but I have just had this issue rear its ugly head on iOS 12 and am very grateful to everyone who posted their solution. For me, I had the same issue as OlesenkoVictor, so setting the background colours in the XIB file to non custom colours has done the trick.

Background color not set in iOS 12
 
 
Q