2013-07-01 23 views
0

我的觀點是一個黑色的矩形...但它不應該是... 這裏是我的繪畫認爲,這是添加到我的表視圖作爲一個子視圖方法:爲什麼我的視圖是黑色的?

- (void)drawRect:(CGRect)rect 
{ 
    //// General Declarations 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    //// Color Declarations 
    UIColor* color3 = [UIColor colorWithRed: 0.102 green: 0.737 blue: 0.612 alpha: 1]; 
    UIColor* buttonStrokeColor = [UIColor colorWithRed: 0.925 green: 0.941 blue: 0.945 alpha: 0.004]; 

    //// Image Declarations 
    UIImage* image = [UIImage imageNamed: @"image"]; 
    UIColor* imagePattern = [UIColor colorWithPatternImage: image]; 

    //// Group 2 
    { 
     //// AddButton Drawing 
     UIBezierPath* addButtonPath = [UIBezierPath bezierPathWithRect: CGRectMake(280.0, 3.0, 30, 30)]; 
     [color3 setFill]; 
     [addButtonPath fill]; 
     [buttonStrokeColor setStroke]; 
     addButtonPath.lineWidth = 1; 
     [addButtonPath stroke]; 

     //// Group 
     { 
      //// Rectangle Drawing 
      UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(280.0, 3.0, 30.0, 30.0)]; 
      CGContextSaveGState(context); 
      CGContextSetPatternPhase(context, CGSizeMake(86, 33)); 
      [imagePattern setFill]; 
      [rectanglePath fill]; 
      CGContextRestoreGState(context); 
      [buttonStrokeColor setStroke]; 
      rectanglePath.lineWidth = 1; 
      [rectanglePath stroke]; 
     } 
    } 

莫不是在使矩形黑色的代碼中的東西? (它是一個全黑盒子)

回答

0

您很可能是在視圖的邊界之外繪圖。所有繪圖必須基於視圖的界限而不是其框架。

這意味着原點始終爲0,0。更新您的基於視圖的原點爲0貝塞爾路徑,0

你應該基於以下觀點的在運行時的大小也設置你的繪製代碼。無論外部代碼創建的視圖大小如何,都可以使代碼正常工作。

+0

謝謝,這工作。順便說一下,當我通過給它一個角落半徑來裁剪視圖時,應該被裁剪的部分充滿了黑色,這並不是我需要的圓形外觀。你知道爲什麼發生了嗎? – EvilAegis

+0

其實nvm我修好了 – EvilAegis

相關問題