为什么用颜色填充矩形时没有任何反应


以下

代码在单击按钮并使用 pushViewController 添加新的 ViewController 后立即调用。 代码位于刚刚添加的新视图控制器中。

- (void)setColorTable
{
    dispatch_async(dispatch_get_main_queue(), ^(){
        [[self view] setNeedsLayout];
        [[self view] layoutIfNeeded];
        float x = _colorBar.frame.origin.x;
        float y = _colorBar.frame.origin.y;

        CGRect bigRect = CGRectMake(x, y, _colorBar.frame.size.width, _colorBar.frame.size.height);
        UIGraphicsBeginImageContext(bigRect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();                
        CGContextSetFillColorWithColor(context, [[UIColor yellowColor] CGColor]);
        CGContextFillRect(context, bigRect);
        UIGraphicsEndImageContext();
    });
}

您不会在代码中绘制$vereywhere。您可以通过发送needToDrawRect:将视图区域标记为需要绘制,然后在自定义-drawRect:实现中绘制,如下所示:

-(void)drawRect:(NSRect)unionRectToDraw
{
  // maybe: [super drawRect:unionRectToDraw];
  float x = _colorBar.frame.origin.x;
  float y = _colorBar.frame.origin.y;
  CGRect bigRect = CGRectMake(x, y, _colorBar.frame.size.width, _colorBar.frame.size.height);
  UIGraphicsBeginImageContext(bigRect.size);
  CGContextRef context = UIGraphicsGetCurrentContext();                
  CGContextSetFillColorWithColor(context, [[UIColor yellowColor] CGColor]);  
  CGContextFillRect(context, bigRect);
  UIGraphicsEndImageContext();
}

最新更新