在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
委托给超类造成了这个黑色的背景问题。
- 在UITableViewCell中的iOS容器视图
- WebCore :: UserGestureIndicator :: processingUserGesture中的EXC_BAD_ACCESS(SIGSEGV)
- Apple开发中的库,API,框架和SDK之间的关系。
- 适用于iOS的Unity3d下的Json.NET
- NSAttributedString书写方向
- IAP:收到`paymentQueueRestoreCompletedTransactionsFinished:`未完成交易的callback
- 上传大背景线程中的图像。
- ARC真的支持iOS 4吗? iOS 4.2 SDK在链接时缺less与ARC相关的符号
- 我们如何改进Conichi的iOS发布过程
- UIButton垂直对齐不起作用
- 在dismissModalViewControllerAnimated之后调用popToRootViewControllerAnimated