Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.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
Ios uiview子类在第一次调用setneedsdisplay时显示为黑色矩形,但之后是ok_Ios_Uiview_Drawrect_Setneedsdisplay - Fatal编程技术网

Ios uiview子类在第一次调用setneedsdisplay时显示为黑色矩形,但之后是ok

Ios uiview子类在第一次调用setneedsdisplay时显示为黑色矩形,但之后是ok,ios,uiview,drawrect,setneedsdisplay,Ios,Uiview,Drawrect,Setneedsdisplay,我有一个自定义UIView子类,它覆盖drawrect。视图从数据源获取数据,并将其中一些信息传递给其委托(视图控制器),以将其处理为需要绘制的字符串。第一次调用setneedsdisplay时,视图显示为一个黑色矩形,但是如果第二次调用它,它就可以正常工作。所有帧大小和位置都设置正确,因此我确信从一开始就正确设置了委托和数据源。以下是有问题的drawrect代码: - (void)drawRect:(CGRect)rect { CGContextRef context = UIGra

我有一个自定义UIView子类,它覆盖drawrect。视图从数据源获取数据,并将其中一些信息传递给其委托(视图控制器),以将其处理为需要绘制的字符串。第一次调用setneedsdisplay时,视图显示为一个黑色矩形,但是如果第二次调用它,它就可以正常工作。所有帧大小和位置都设置正确,因此我确信从一开始就正确设置了委托和数据源。以下是有问题的drawrect代码:

- (void)drawRect:(CGRect)rect
{

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 2.0);
    CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
    CGFloat components[] = {0, 0, 0, 1.0};
    CGColorRef color = CGColorCreate(colorSpace, components);
    ;
    //CGContextStrokeRect(context, CGRectMake(1, 1, self.frame.size.width-1.0, self.frame.size.height-1.0));

    CGContextMoveToPoint(context, 0, [_delegate denominatorPoint:self].y);
        if ([_delegate numeratorBiggerThanDenominator:self]==YES) 
    CGContextAddLineToPoint(context,[[_delegate numeratorString:self]sizeWithFont:[_delegate fontToDrawWith:self]].width , [_delegate denominatorPoint:self].y);
        else
            CGContextAddLineToPoint(context,[[_delegate denominatorString:self]sizeWithFont:[_delegate fontToDrawWith:self]].width , [_delegate denominatorPoint:self].y);
    if ([_delegate hasAFraction:self]==YES) {


    CGContextSetStrokeColorWithColor(context, color);


    CGContextStrokePath(context);

    }

    CGColorSpaceRelease(colorSpace);
    CGColorRelease(color);



    self.backgroundColor=[UIColor clearColor];


    [[_delegate numeratorString:self] drawAtPoint:[_delegate numeratorPoint:self] withFont:[_delegate fontToDrawWith:self]];
    NSLog([_delegate numeratorString:self]);
    if ([_delegate hasAFraction:self]==YES) 
        [[_delegate denominatorString:self] drawAtPoint:[_delegate denominatorPoint:self] withFont:[_delegate fontToDrawWith:self]];
    [[_delegate Variable:self] drawAtPoint:[_delegate variablePoint:self] withFont:[_delegate fontToDrawWith:self]];
    if ([_delegate hasAParenthesTerm:self]==YES) {
        //NSLog(@"parentheses being drawn");
        NSString* openParentheses=@" (";
        [openParentheses drawAtPoint:[_delegate openParenthesesPoint:self] withFont:[_delegate fontToDrawWith:self]];
        NSString* closedParentheses=@")";
        [closedParentheses drawAtPoint:[_delegate closeParenthesesPoint:self] withFont:[_delegate fontToDrawWith:self]];
    }

}

让我知道是否有其他代码可以帮助解决问题。

您是否尝试移动
self.backgroundColor=[UIColor clearColor]到init方法


无论如何,你只需要运行一行,我可以想象在绘制方法中间调用它可能会导致一个问题(并且在第二次调用DRAGREST中,背景色已经被设置为Curror颜色,所以UIVIEW可以忽略它,使你的问题消失在第二个StestDunsStand调用中)。

完全奏效了,太棒了!仍然神秘的是,它会在第二集工作,但需要显示给我。很高兴它有帮助!正如我所提到的,这可能是因为UIView可以在对drawRect的第二次调用中忽略这一行,因为您没有在那里更改颜色,显然我不是一个非常仔细的读者。可能是什么让我一开始就有麻烦了!