在UIScrollView中重写drawRect时的黑色背景

所以我想重写我的UIScrolLView drawRect,但它给了我这个黑色的背景,而不是我为我的UIScrollView指定的背景颜色。 为什么是这样? 如果我删除drawRect代码,那么一切都很好:

- (void)drawRect:(CGRect)rect { [super drawRect:rect]; if (shouldDrawVerticalLineForProfile){ CGContextRef context = UIGraphicsGetCurrentContext(); CGColorRef separatorColor = [UIColor colorWithRed:47.0/255.0 green:47.0/255.0 blue:47.0/255.0 alpha:1.0].CGColor; // Add at bottom CGPoint startPoint = CGPointMake(60, 0); CGPoint endPoint = CGPointMake(60, 10000); CGContextSaveGState(context); CGContextSetLineCap(context, kCGLineCapSquare); CGContextSetStrokeColorWithColor(context, separatorColor); CGContextSetLineWidth(context, 5.0); CGContextMoveToPoint(context, startPoint.x + 0.5, startPoint.y + 0.5); CGContextAddLineToPoint(context, endPoint.x + 0.5, endPoint.y + 0.5); CGContextStrokePath(context); CGContextRestoreGState(context); } } 

我猜你正在寻找的是:

 myScrollViewInstance.opaque = NO 

之后,您的滚动视图的背景不应该是黑色了。

在我的UIScrollView子类中重写drawRect时遇到了类似的情况。

简单地覆盖:

 - (void)drawRect:(CGRect)rect { [super drawRect:rect]; } 

导致了一个黑色的背景,而不是所需的清晰的背景,我会得到没有压倒一切。

我发现这是因为在界面生成器中将视图背景设置为默认值,并将其设置为Clear Color为我解决了这个问题。

我不确定这是否与您的问题有关,但我认为我会分享,以防止它可能有帮助。

这应该有所帮助

 CGContextSetFillColorWithColor(context, colorBack); CGContextFillRect(context, self.bounds); 

//select相应的边界和颜色

请尝试这个工作给我

  - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setBackgroundColor:[UIColor clearColor]]; } return self; } - (void)drawRect:(CGRect)rect { [super drawRect:rect]; if (shouldDrawVerticalLineForProfile){ CGContextRef context = UIGraphicsGetCurrentContext(); CGColorRef separatorColor = [UIColor colorWithRed:47.0/255.0 green:47.0/255.0 blue:47.0/255.0 alpha:1.0].CGColor; // Add at bottom CGPoint startPoint = CGPointMake(60, 0); CGPoint endPoint = CGPointMake(60, 10000); CGContextSaveGState(context); CGContextSetLineCap(context, kCGLineCapSquare); CGContextSetStrokeColorWithColor(context, separatorColor); CGContextSetLineWidth(context, 5.0); CGContextMoveToPoint(context, startPoint.x + 0.5, startPoint.y + 0.5); CGContextAddLineToPoint(context, endPoint.x + 0.5, endPoint.y + 0.5); CGContextStrokePath(context); CGContextRestoreGState(context); } } 

这里的答案非常清楚地记载在UIView的drawRect:方法的AppleDocs中。 第一句话是:

这个方法的默认实现什么也不做。

所以,这里打电话给super的build议不会有帮助。 答案的其余部分在讨论中进一步包含在内:

…如果视图的opaque属性设置为YES,则drawRect:方法必须完全填充具有不透明内容的指定矩形。

这意味着如果你不打算有一个透明的背景,你需要实际绘制背景。 最正确的方法是使用下面的drawRect: template:

 - (void)drawRect:(CGRect)rect { [super drawRect:rect]; // optional if a direct UIView-subclass, should be called otherwise. CGContextRef context = UIGraphicsGetCurrentContext(); // Fill the background color, if needed if (self.opaque) { UIGraphicsPushContext(context); CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor); CGContextFillRect(context, rect); UIGraphicsPopContext(); } // Your code here... } 

唯一对我有效的是在setNeedsDisplay调用之后显式设置背景。 在我的例子中,我在地图上使用了MKAnnotationView的子类,而不是UIScrollView ,所以我不太清楚这适用于OP场景。 在我的情况下,我从setAnnotation调用setNeedsDisplay ,并且我发现这样做通过backgroundColor重置。 setNeedsDisplay修复问题后,只需通过backgroundColor重新设置即可。

FWIW,我也观察到,简单地覆盖drawRect委托给超类造成了这个黑色的背景问题。