UIRectFill在iOS 7中不起作用

时间:2013-10-11 09:03:52

标签: iphone ios ipad ios6 ios7

尝试在UITableViewCell中绘制一个rect

//Works with iOS6 and earlier but NOT with ( iOS7 )

 - (void)drawRect:(CGRect)rect {
    // Creating a black border
    [[UIColor blackColor] setFill];
    UIRectFill(CGRectMake(10, 5, 40, 43));

    // Filling with rig color
    [[UIColor colorWithRed:r green:g blue:b alpha:a] setFill];
    UIRectFill(CGRectMake(11, 6, 38, 41));
}

有人知道为什么这在iOS 7中不起作用,但在iOS 6中起作用吗?

2 个答案:

答案 0 :(得分:1)

我在iOS 7下遇到了同样的问题 - 您在-drawRect方法中绘制的任何内容都会被单元格的子视图遮挡。相反,将新视图子类的实例作为子视图添加到单元格contentView并在那里进行绘制。

请参阅thisthis。如果您不想创建自定义子类,则可以改为使用block drawing views

答案 1 :(得分:0)

我已经通过向contentview

添加子视图来修复它
- (id)initWithStyle:(UITableViewCellStyle)style 
                              reuseIdentifier:(NSString *)reuseIdentifier{
      if(!self)
         return self;

      self.colorView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 40, 43)];
      self.colorView.layer.borderColor = [[UIColor blackColor] CGColor];
      self.colorView.layer.borderWidth = 1.0;
      [self.contentView addSubview:self.colorView];

  }

- (void)setActivityColor:(UIColor*)color
{
    [self.colorView setBackgroundColor:color];
}