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);
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);
}
override func draw(_ rect: CGRect) {
let r = CGRect(x: 5, y: 5, width: 10, height: 10)
UIColor.yellow.set()
UIRectFill(r)
}
that's it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With