Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未调用Quartz iPhone drawRect_Iphone_Quartz Graphics_Drawrect - Fatal编程技术网

未调用Quartz iPhone drawRect

未调用Quartz iPhone drawRect,iphone,quartz-graphics,drawrect,Iphone,Quartz Graphics,Drawrect,我正在子类化UIView,并通过详图视图控制器调用它。当我加载它时,它显示了正确的尺寸和位置,但它只是一个黑色的正方形。。。当我将UIView与类一起放入interface builder时,它就可以工作了。我不认为draw rect被称为。这是我的密码: - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization cod

我正在子类化UIView,并通过详图视图控制器调用它。当我加载它时,它显示了正确的尺寸和位置,但它只是一个黑色的正方形。。。当我将UIView与类一起放入interface builder时,它就可以工作了。我不认为draw rect被称为。这是我的密码:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setNeedsDisplay];
    }
    return self;
}


- (void)drawRect:(CGRect)rect
{
    // Drawing code

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 255.0, 255.0, 0, 1);
    CGContextStrokeRect(context, CGRectMake(0, 0, 50, 50));
}
我这样称呼它:

 GameBox *boxy = [[GameBox alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
    [self.view addSubview: boxy];

您正在设置填充颜色,但正在笔划矩形。您需要设置笔划颜色:
cgcontextsetrgbstrockecolor

为什么要在init中调用setneedsdisplay?这是不正确的,在初始化的时候它还没有被添加到任何视图中,并且视图还没有加载..那么我如何让它显示为非黑色呢?我在没有设置setneedsdisplayfirst的情况下尝试过,颜色介于0和1之间,而不是255。。。