Setting background color of UIView subclass doesn&

2019-06-18 23:29发布

问题:

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

The relevant code looks like this:

[...]
@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;
}

回答1:

This is indicative of the view not having a frame set to it. I recommend setting a breakpoint in your initWithFrame: to verify that its being called. If you were to call, say, ... = [UIView alloc] init], then that could be the source of your problem.

EDIT

If initWithFrame: is in fact being called, it's possible that the view is being covered by another view giving the appearance that it's not working (since you don't see it) or that the view itself is hidden.

Another way to troubleshoot is to override the backgroundColor property and set a breakpoint. Find out what else, in the callstack, is changing the color.



回答2:

If this view is being unarchived from a xib, you need to override -initWithCoder:. -initWithFrame: is only invoked if you are creating your view programmatically.



回答3:

I had this same problem. I hooked into layoutSubviews() and it worked ok:

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


回答4:

In what method do you call self.myView.backgroundColor? Are you sure, that it's after viewDidLoad: ? But, have no idea what is wrong with your first method. Could you show more code?



回答5:

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



回答6:

You may use self.layer.backgroundColor instead:

mySubclassedView.layer.backgroundColor = UIColor.green.cgColor


回答7:

I just had this exact same problem. The background color did not show even though I set the correct frame and set the background color to white in my custom init method, as well as in my viewWillAppear method. I also verified that nothing was covering it.

Then I found the solution: I set the background color in viewDidAppear instead, and all was fine:

- (void)viewDidAppear:(BOOL)animated
{
   [self.view setBackgroundColor:[UIColor whiteColor]];
   self.view.frame = _viewFrame;
}

(The _viewFrame CGRect was passed in to my init method.)

Another option is to set it in - (void)viewDidLayoutSubviews, depending on when and how exactly you want to set your background color.

To be entirely honest, I don't understand (yet) why setting the background color in viewDidAppear worked while it didn't work in the init method, and the code for setting it was identical in both places.

Hope this helps,

Erik


UPDATE: It does have something to do with view's frame. When I set my view's frame in my init method, then setting the background color in the viewDidAppear no longer has the desired effect. This is even the case if I set my view's frame after my view build method that creates the sub views. So the real mystery is: between the point where I am done creating my view and the point where it is displayed, what in the view's life cycle is causing the view's frame to be reset to something that is incorrect?

So, the answer really is: it will work as long as your frame is set correctly and your view is visible. Just check your view's frame throughout the view's lifecycle to make sure it's correct.

Tricky...