简体   繁体   English

设置UIView子类的背景颜色不起作用

[英]Setting background color of UIView subclass doesn't work

I'm trying to change background color of one of my UIView subclasses. 我正在尝试更改我的一个UIView子类的背景颜色。 For some reason self.backgroundColor = [UIColor whiteColor]; 出于某种原因, self.backgroundColor = [UIColor whiteColor]; doesn't do anything when I put it in my - (id)initWithFrame:(CGRect)frame method inside the view. 当我把它放在我的- (id)initWithFrame:(CGRect)frame方法中时,它没有做任何事情。 The view is always black. 视图始终是黑色的。 I have also tried self.myView.backgroundColor ... from my view's controller, but that didn't work either. 我也从我的视图控制器尝试了self.myView.backgroundColor ...但是这也没有用。 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;
}

I had this same problem. 我有同样的问题。 I hooked into layoutSubviews() and it worked ok: 我迷上了layoutSubviews() ,它工作正常:

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

If this view is being unarchived from a xib, you need to override -initWithCoder: . 如果要从xib取消归档此视图,则需要覆盖-initWithCoder: . -initWithFrame: is only invoked if you are creating your view programmatically. -initWithFrame:仅在以编程方式创建视图时才会调用。

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. 我建议在initWithFrame:设置断点initWithFrame:验证它是否被调用。 If you were to call, say, ... = [UIView alloc] init] , then that could be the source of your problem. 如果您打电话,比如说, ... = [UIView alloc] init] ,那么这可能是您问题的根源。

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. 如果实际上正在调用initWithFrame:那么该视图可能会被另一个视图覆盖,从而使其看起来不起作用(因为您没有看到它)或者视图本身是隐藏的。

Another way to troubleshoot is to override the backgroundColor property and set a breakpoint. 另一种排除故障的方法是覆盖backgroundColor属性并设置断点。 Find out what else, in the callstack, is changing the color. 找出callstack中正在改变颜色的其他内容。

In what method do you call self.myView.backgroundColor? 你用什么方法调用self.myView.backgroundColor? Are you sure, that it's after viewDidLoad: ? 你确定,它是在viewDidLoad之后:? But, have no idea what is wrong with your first method. 但是,不知道你的第一种方法有什么问题。 Could you show more code? 你能展示更多代码吗?

您可以使用self.layer.backgroundColor

mySubclassedView.layer.backgroundColor = UIColor.green.cgColor

Why can't you implement self.backgroundColor = [UIColor whiteColor] in -viewDidLoad method instead of -initWithFrame ? 你为什么不能执行self.backgroundColor = [UIColor whiteColor]-viewDidLoad方法,而不是-initWithFrame Then try self.backgroundColor = [UIColor whiteColor]; 然后尝试self.backgroundColor = [UIColor whiteColor]; as well as self.myView.backgroundColor to see which works. 以及self.myView.backgroundColor以查看哪些有效。

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. 即使我在自定义init方法中设置了正确的帧并将背景颜色设置为白色,以及在我的viewWillAppear方法中,也没有显示背景颜色。 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: 然后我找到了解决方案 :我在viewDidAppear设置背景颜色,而且一切都很好:

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

(The _viewFrame CGRect was passed in to my init method.) _viewFrame CGRect已传入我的init方法。)

Another option is to set it in - (void)viewDidLayoutSubviews , depending on when and how exactly you want to set your background color. 另一种选择是将其设置在- (void)viewDidLayoutSubviews ,具体取决于您想要设置背景颜色的时间和方式。

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. 说实话,我还不明白为什么在viewDidAppear设置背景颜色虽然在init方法中不起作用,但在两个地方设置它的代码是相同的。

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. 当我在init方法中设置视图的框架时,在viewDidAppear中设置背景颜色不再具有所需的效果。 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... 整蛊......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM