为什么自定义drawRect未在6.1模拟器中显示?

时间:2013-09-17 06:28:24

标签: ios ios-simulator drawrect

ThumbView中有一个UIView子类(UICollectionViewCell)。以下代码在iOS 7模拟器中运行良好:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    ThumbView *thumbView = (ThumbView *)[cell viewWithTag:1];
    [thumbView setNeedsDisplay];
    return cell;
}

enter image description here

然而,在iOS 6版本的模拟器中:

网格全部消失了:

enter image description here

我在ThumbView的NSLog中添加了drawRect,以确保调用drawRect

有什么问题?

  • Xcode版本5.0(5A1412)
  • iOS模拟器版本7.0(463.9.4)

更新(图纸代码):

- (void)drawRect:(CGRect)rect
{
    NSLog(@"%s", __func__);
   [self drawGrid:rect];
}

- (void)drawGrid:(CGRect)rect
{
    CGFloat margin = self.margin;
    CGFloat lineWidth = 2.0;

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect gridRect = CGRectInset(rect, margin, margin);

    for (NSInteger i = 1; i < 9; i++) {
        CGFloat h = gridRect.size.height / 9;
        CGFloat y = i * h;
        CGContextMoveToPoint(context, margin, y+margin);
        CGContextAddLineToPoint(context, gridRect.size.width+margin, y+margin);
        CGContextSetLineWidth(context, lineWidth/4.0);
        if (i == 3 || i == 6) {
            CGContextSetStrokeColorWithColor(context, UIColor.darkGrayColor.CGColor);
        } else {
            CGContextSetStrokeColorWithColor(context, UIColor.lightGrayColor.CGColor);
        }
        CGContextStrokePath(context);
    }

    for (NSInteger i = 1; i < 9; i++) {
        CGFloat w = gridRect.size.width / 9;
        CGFloat x = i * w;
        CGContextMoveToPoint(context, x+margin, margin);
        CGContextAddLineToPoint(context, x+margin, gridRect.size.height+margin);
        CGContextSetLineWidth(context, lineWidth/4.0);
        if (i == 3 || i == 6) {
            CGContextSetStrokeColorWithColor(context, UIColor.darkGrayColor.CGColor);
        } else {
            CGContextSetStrokeColorWithColor(context, UIColor.lightGrayColor.CGColor);
        }
        CGContextStrokePath(context);
    }

    CGFloat frameWidth = lineWidth * 2.0;
    CGRect frameRect = CGRectInset(rect, 0.0, 0.0);
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) frameWidth = lineWidth * 2.0;
    CGContextSetLineWidth(context, frameWidth);
    CGContextSetStrokeColorWithColor(context, UIColor.darkGrayColor.CGColor);
    CGContextStrokeRect(context, frameRect);
}

2 个答案:

答案 0 :(得分:2)

没有看到代码,很难说。当我在视网膜和非视网膜模拟器上运行代码并且没有正确考虑尺度差异时,我已经看到过这样的事情。

答案 1 :(得分:1)

您是否检查过iOS 6中的白色背景上没有画出白线? (例如,如果您在iOS 7中使用tintColor ...)