0

I'm trying to learn how to make child view controller and face the problem: child view controller I made for some unknown reasuns occupies the whole screen instead of view to which I add it. Here is my super-simple code:

CVCChildViewController *childViewController =
    [[self storyboard] instantiateViewControllerWithIdentifier:kVCIdentifier];
[self addChildViewController:childViewController];
[self.childView addSubview:childViewController.view];
[childViewController didMoveToParentViewController:self];

And here is storyboard screen: enter image description here

White UIView (childView) is subview of self.view. And I wand child view controller does not cross this childView bounds. How could I do this?

Maria
  • 755
  • 1
  • 11
  • 29

3 Answers3

1

Set your childViewController.view.bounds = self.childView.bounds before you add as a subview. Basically you need to set the frame of your childViewControllers view before adding it as a subview , else it would take its default height. I hope this helps. Cheers

NavinDev
  • 995
  • 1
  • 7
  • 8
  • I should use didMoveToParentViewController right after addChildViewController. That was the first mistake. And the second - you right, I should set frame for child view controller. But I still don't understand why child view controller ignored it's height from IB. – Maria May 19 '14 at 09:05
0

Why you doing that programmatically? In storyboard add Container View (instead View) and attach your CVCChildViewController to it. This should do the trick.

To access sub controller just provide respective IBOutlet.

enter image description here

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • Yes, it works fine in IB, but I want to learn how to do it programmatically - just for fun. – Maria May 19 '14 at 08:50
0

enter image description here

Here was my problem. "Resize View From NIB" should be unchecked for child view controller. After that it will be with correct frame. That's because this option make viewController.view.frame equal to applicationFrame by default.

Maria
  • 755
  • 1
  • 11
  • 29