I had the same problem. The background color did not display, even if I set the correct frame and set the background color to white in my normal initialization method, as well as in my viewWillAppear method. I also confirmed that nothing covered.
Then I found a solution : instead, I set the background color to viewDidAppear , and everything was fine:
- (void)viewDidAppear:(BOOL)animated { [self.view setBackgroundColor:[UIColor whiteColor]]; self.view.frame = _viewFrame; }
( _viewFrame CGRect was passed to my init method.)
Another option is to set it to - (void)viewDidLayoutSubviews , depending on when and how exactly you want to set the background color.
To be completely honest, I donβt understand yet why the background color setting in viewDidAppear worked while it did not work in the init method, and the code for installing it was identical in both places.
Hope this helps,
Eric
UPDATE: It has something to do with frame representation. When I set my view in my init method, then setting the background color in viewDidAppear no longer has the desired effect. This is even the case if I set my view frame after my build method of the assembly, which creates sub-views. So, the real mystery: between where I finished creating my view and the point where it is displayed, what causes the view frame to reset something wrong in the view life cycle?
So, the answer is in fact: it will work until your frame is set correctly and your view is visible. Just check the viewing frame throughout the viewing life cycle to make sure it is correct.
Tricky ...
source share