为什么在用颜色填充Rect时没有任何反应?

时间:2017-01-04 11:42:54

标签: ios objective-c iphone

在单击按钮并使用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();
    });
}

1 个答案:

答案 0 :(得分:0)

您不会在代码中绘制$ 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();
}