2013-03-11 59 views
-1

我只想在視圖上繪製矩形。這是我的UIView子類中的代碼:drawRect不繪製矩形 - Objective-C

- (void)drawRect:(CGRect)rect 
{ 
context = UIGraphicsGetCurrentContext(); 
CGContextSetRGBFillColor(context, 255.0/255.0, 0.0/255.0, 0.0/255.0, 1); 
CGContextAddRect(context, (CGRectMake(20, 20, 20, 20))); 

} 

當我運行它時,沒有繪製矩形。怎麼了?

+1

因爲它不是通過繪製:) – 2013-03-11 08:13:01

+1

請閱讀所使用功能的文檔:) – Sulthan 2013-03-11 11:33:37

回答

2

CGContextAddRect只是爲上下文添加了一個矩形路徑。您還需要使用CGContextFillPathCGContextStrokePath中風或填充路徑。

您也可以直接用UIRectFillCGContextFillRect填寫矩形。