2012-07-03 34 views
18

所以我想重寫我的UIScrolLView drawRect,但它給了我這個黑色的背景,而不是我爲我的UIScrollView指定的背景顏色。爲什麼是這樣?如果我刪除的drawRect代碼,然後一切都很好:重寫drawRect在UIScrollView黑色背景

- (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); 

    } 

} 
+0

我覺得backgroundColor屬性簡單地包裝了CGContextAddRect和CGContextFillPath,如果你做這樣的事情是什麼? – 2012-07-03 20:58:20

+0

做點什麼? – xonegirlz

+0

使用上面提到的兩個函數手動繪製bgcolor。 – 2012-07-03 21:05:31

回答

4

這應該有助於

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

//選擇範圍和colorBack相應

+0

使用rect,不允許UIKit在需要時優化繪圖。 –

19

我猜測你正在尋找的是:

myScrollViewInstance.opaque = NO 

之後,您的滾動視圖的背景不應該是黑色了。

+1

我試過但沒有幫助。我認爲這不是背景是黑色的。只有當scrollView的drawRect被覆蓋時,scrollView纔會變黑。如果drawRect沒有被覆蓋,那麼滾動視圖是正常的白色。 – coolcool1994

+0

此答案僅適用於您實際需要半透明背景的情況。如果您希望尊重不明確的'backgroundColor',它將會失敗。 –

10

在我的UIScrollView子類中重寫drawRect時遇到了類似的情況。

與簡單地重寫:

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

導致了黑色的背景,而不是所期望的清晰的背景我會得到不覆蓋。

我發現這是因爲視圖背景在界面生成器中設置爲默認值,並將其設置爲Clear Color爲我解決了這個問題。

我不確定這是否與您的問題有關,但認爲我會分享以防它可能有所幫助。

+1

UIView的'drawRect:'記錄爲「此方法的默認實現什麼都不做」,所以調用它肯定不會做任何事情。你必須自己填寫背景。 –

-1

對我來說唯一的工作是在setNeedsDisplay的任何調用後明確設置背景。在我的例子中,我在地圖上使用MKAnnotationView的子類,而不是UIScrollView,所以我沒有真的知道這適用於OP場景。在我的情況下,我打電話setNeedsDisplaysetAnnotation,我發現這樣做重置backgroundColor。在setNeedsDisplay修復問題後,只需重新設置backgroundColor即可。

FWIW,我也觀察到,簡單地覆蓋drawRect委託給超類造成了這個黑色背景問題。

4

請嘗試這個公司對我

- (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); 

     } 

    } 
4

這裏的答案是在AppleDocs爲的UIView的drawRect:方法非常清楚地記錄工作。第一句話是:

此方法的默認實現什麼都不做。

因此,這裏給超級電話打電話的建議不會有幫助。答案的其餘部分被包含在討論中進一步下跌:

...如果您的視圖的不透明屬性設置爲YES,你drawRect:方法必須完全填充不透明內容指定的矩形。

這意味着如果你不打算有一個透明的背景,你需要實際繪製背景。這樣做的最正確的方法是使用下面的drawRect:模板:

- (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... 
}