如何绘制矩形? [英] How to draw a rectangle?

查看:97
本文介绍了如何绘制矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在viewContoller的视图中绘制一个填充的矩形. 我在viewDidLoad中编写了以下代码.但是没有变化. 怎么了?

I want to draw a filled rectangle in my viewContoller's view. I wrote the code below in viewDidLoad. But there is no change. What is wrong?

CGRect rectangle = CGRectMake(0, 100, 320, 100);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextFillRect(context, rectangle);

推荐答案

您无法在viewController中执行此操作. 您需要扩展View并在"drawRect:"下添加代码.

You can't do it in a viewController. You need to extend your View and add the code under "drawRect:"

这将改变视图的绘制逻辑.

this will change the drawing logic of your view.

-(void) drawRect:(CGRect)rect{    
[super drawRect:rect];  
    CGRect rectangle = CGRectMake(0, 100, 320, 100);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextFillRect(context, rectangle);
}

这篇关于如何绘制矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆