【问题标题】:Cannot get correct UIColor in rectangle无法在矩形中获得正确的 UIColor
【发布时间】:2014-03-24 11:29:44
【问题描述】:

全部,

我有这段代码:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    CGContextFillRect(context, CGRectMake(0, 440, 320, 30));

我这样称呼它:

_rectangeView = [[PopUpRectangle alloc] initWithFrame:CGRectMake(0, 538, 320, 30)];

但它是黑色的!

有人建议这是为什么?

【问题讨论】:

  • 这里面rect的值是多少。我怀疑您正在着色的矩形实际上不在视图的矩形范围内。 bounds != frame 等等。 NVM,添加了一个答案。
  • DrawRect: 有没有接到电话???在里面放一个NSLog(),看看是不是。

标签: ios objective-c core-graphics


【解决方案1】:

像这样改变你的着色矩形......

CGContextFillRect(context, CGRectMake(0, 0, 320, 30));

使用 0, 0 作为原点,而不是 0, 440。

应该可以的。

在 drawRect 内部,你处理的是视图本身的向量空间,而不是它的父视图。所以 0, 0 是你的rectangleView 的左上角。

【讨论】:

    【解决方案2】:

    您的_rectangeView 高度仅为 30 点,但上下文填充矩形边界 y 值为 440.0;超出了视图的范围。尝试将 y 值更改为 0,您将看到颜色。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      • 2016-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多