Ios 在屏幕上绘制矩形时遇到问题
我正在跟随pluralsight的视频教程,它在屏幕上绘制了一个红色矩形。我有一个名为PSViewDemo的UIView子类,在.m文件中有以下代码:Ios 在屏幕上绘制矩形时遇到问题,ios,objective-c,Ios,Objective C,我正在跟随pluralsight的视频教程,它在屏幕上绘制了一个红色矩形。我有一个名为PSViewDemo的UIView子类,在.m文件中有以下代码: // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { CGCon
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, [UIColor redColor].CGColor);
CGContextFillRect(context, CGRectMake(40, 400,100,200));
// Drawing code
}
我在viewcontroller的Viewdidload中为应用程序中的唯一视图调用它(直到代码添加子视图)
整个程序编译和运行没有错误,但屏幕上没有红色矩形
我错过了什么?我很确定我完全遵循了教程,所以也许自教程制作以来Cocoa发生了一些变化?我正在使用xCode 5。更改
CGContextSetFillColor(context, [UIColor redColor].CGColor);
到
CGContextSetFillColorWithColor(上下文,[[UIColor whiteColor]CGColor])
或使用CGColorGetComponents
,如下面的答案所示。我今天在文档中读到,CGContextSetFillColorWithColor()
是首选。
CGContextSetFillColor(context, [UIColor redColor].CGColor);
CGContextSetFillColor(context, CGColorGetComponents([[UIColor redColor] CGColor]));