Setting the background color of a subclass of UIView does not work

I am trying to change the background color of one of my subclasses of UIView. For some reason, self.backgroundColor = [UIColor whiteColor]; Does nothing when I put it in my method - (id)initWithFrame:(CGRect)frame inside the view. The performance is always black. I also tried self.myView.backgroundColor ... from my controller, but that didn't work either. Any ideas on what I'm doing wrong?

The corresponding code is as follows:

 [...] @interface PaperView : UIView [....] [...] @implementation PaperView [...] - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [...] // Initialization code self.backgroundColor = [UIColor whiteColor]; // This doesn't do anything, the view is always black. } return self; } 
+6
source share
7 answers

This indicates a view that has no scope on it. I recommend setting a breakpoint in initWithFrame: to make sure it is called. If you need to call, say ... = [UIView alloc] init] , this may be the source of your problem.

EDIT

If initWithFrame: is actually called, it is possible that the view is covered by another view giving the view that it is not working (because you cannot see it) or that the view itself is hidden.

Another troubleshooting method is to override the backgroundColor property and set a breakpoint. Find out what else, in callstack, changes color.

+3
source

If this view is removed from xib, you need to override -initWithCoder: -initWithFrame: only called when you programmatically create your view.

+7
source

I had the same problem. I connected to layoutSubviews() and it worked fine:

 override func layoutSubviews() { super.layoutSubviews() self.backgroundColor = UIColor.clearColor() } 
+2
source

In which method do you call self.myView.backgroundColor? Are you sure after viewDidLoad :? But, I don’t know what is wrong with your first method. Could you show more code?

+1
source

Why can't you implement self.backgroundColor = [UIColor whiteColor] in -viewDidLoad instead of -initWithFrame ? Then try self.backgroundColor = [UIColor whiteColor]; as well as self.myView.backgroundColor to see what works.

0
source

Instead, you can use self.layer.backgroundColor :

 mySubclassedView.layer.backgroundColor = UIColor.green.cgColor 
0
source

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 ...

-1
source

Source: https://habr.com/ru/post/904623/


All Articles