Maria
Maria

Reputation: 765

Why is child view controller occupies the entire screen?

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?

Upvotes: 0

Views: 2239

Answers (3)

Maria
Maria

Reputation: 765

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.

Upvotes: 0

Marek R
Marek R

Reputation: 37512

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

Upvotes: 0

NavinDev
NavinDev

Reputation: 995

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

Upvotes: 1

Related Questions