3

I have a ViewController that has 2 NSViews in it of different sizes. I'm wanting to add the view of a custom ViewController as a subView to both of these NSViews and have it dynamically size to fill (the 2 parent views). I can accomplish this just fine in the implementation file for the main layout, but its a lot of code. I would like to instead have my custom ViewController do all the work. To do that, I need to know the height and width of the view that I am adding my custom view to. There is a parentViewController property, but it isn't doing anything for me. Is there a way to reference the view that a view is being added to?

In my custom ViewController viewDidLoad method I would like to be able to have

[self.view setFrame:CGRectMake(0, 0, 
    self.parentViewController.view.frame.size.width, 
    self.parentViewController.view.frame.size.height)];

but the height and width are both nil here.

I've been digging through documentation for hours and I am still confused. Any help would be much appreciated.

Ethan
  • 628
  • 2
  • 6
  • 20

1 Answers1

4

The parentViewController property should be set in the first place. It will be nil if it is not set. You have not specified if it is set correctly. There is a simpler way to do this though, assuming that your view's superview is set correctly. Try this:

[self.view setFrame:self.superview.frame];

or make the rect from the superview's frame if you need to alter something in there.

MadhavanRP
  • 2,832
  • 1
  • 20
  • 26
  • Would I set the parentViewController in the viewDidLoad method of my custom view controller? Thanks for the tip too! – Ethan Dec 05 '11 at 18:28
  • you could do that if you maintain a reference to it in any global variable or get it from any other method. I would say that you should set it when you alloc and init your custom view controller – MadhavanRP Dec 06 '11 at 05:33