设置 UIView 子类的背景颜色不起作用 [英] Setting background color of UIView subclass doesn't work

查看:57
本文介绍了设置 UIView 子类的背景颜色不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改我的 UIView 子类之一的背景颜色.出于某种原因,self.backgroundColor = [UIColor whiteColor];当我把它放在我的 - (id)initWithFrame:(CGRect)frame 方法里面时,它什么也没做看法.视图始终是黑色的.我也从我的视图控制器中尝试了 self.myView.backgroundColor ... ,但这也不起作用.关于我做错了什么的任何想法?

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?

相关代码如下:

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

推荐答案

这表示视图没有设置框架.我建议在您的 initWithFrame: 中设置一个断点以验证它是否被调用.如果您要调用,例如,... = [UIView alloc] init],那么这可能是您问题的根源.

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.

编辑

如果 initWithFrame: 实际上正在被调用,则视图可能被另一个视图覆盖,看起来它不起作用(因为您没有看到它)或视图本身是隐藏的.

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.

另一种排除故障的方法是覆盖 backgroundColor 属性并设置断点.找出调用堆栈中还有什么正在改变颜色.

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.

这篇关于设置 UIView 子类的背景颜色不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆