Setting background color of UIView subclass doesn&

2019-06-18 23:07发布

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;
}

7条回答
SAY GOODBYE
2楼-- · 2019-06-18 23:17

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?

查看更多
Animai°情兽
3楼-- · 2019-06-18 23:17

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

查看更多
对你真心纯属浪费
4楼-- · 2019-06-18 23:28

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.

查看更多
啃猪蹄的小仙女
5楼-- · 2019-06-18 23:28

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.

查看更多
Viruses.
6楼-- · 2019-06-18 23:28

You may use self.layer.backgroundColor instead:

mySubclassedView.layer.backgroundColor = UIColor.green.cgColor
查看更多
一纸荒年 Trace。
7楼-- · 2019-06-18 23:29

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

override func layoutSubviews() {
    super.layoutSubviews()
    self.backgroundColor = UIColor.clearColor()
}
查看更多
登录 后发表回答