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

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

问题描述

我想在viewContoller的视图中绘制填充矩形。
我在viewDidLoad中写了下面的代码。但没有变化。
有什么问题?

I want to draw filled rectangle in my viewContoller's view. I wrote below code 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中执行此操作。
您需要扩展视图并在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天全站免登陆