Search code examples
uiviewsubviewquartz-2ddrawrect

How to draw into a subview with Quartz2D


What I did earlier:
Created a new project in XCode to test out Quartz2D.
Created an UIView, overwrote drawRect and made some graphics with it.

What I want to do:
Integrate this UIView in a bigger project. The hierarchy is like this:
View1Controller->View1->View2->View3 (A view in a view in a view)
I want my View3 to display those graphics.

What I tried:
Created a new class according to the TestProject that implements the UIView interface.
...Replaced the old UIView for View3 in the parent with myClass. But after that I could not link to my storyboard UIView.
...Did not replace it, but assigned myClass (constructor with a CGRect) to it in the parent's constructor.
But I cannot see my graphics.

I am very new to objective-C, and I've read a couple of answers that go along the lines: "You should simply overwrite the drawRect() function for your View3". I am confused how to accomplish this and detailed answers would be veeery helpful. Thank you. :)


Solution

  • Simply add your custom UIView as subview to the viewcontroller or one of its subviews.

    MyView *myView = [[MyView alloc] initWithFrame: CGRectMake(0,0,100,100)];
    [self.view addSubView:myView];