-2

Code:

UINavigationController * childNaviVC = [[UINavigationController alloc] initWithRootViewController:self.subVC];
[self addChildViewController:childNaviVC];
[self.view addSubview:childNaviVC.view];
[childNaviVC.view setFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
self.subVC.view.frame = childNaviVC.view.frame;
[childNaviVC.view addSubview:self.subVC.view];
[childNaviVC didMoveToParentViewController:self];

what i want to do is add a UINavigationController (childNaviVC) as sub viewcontroller (subVC) of the designated VC, and use the UINavigationController's root viewController to display the content; the problem which i met is UINavigationController's root view(subVC) size is consistent with UINavigationController's parent VC

the red view size isn't equal to the cyan enter image description here

thanks

Sudo
  • 991
  • 9
  • 24
Snail
  • 3,046
  • 1
  • 9
  • 12

2 Answers2

0

One issue is that you are double adding the subview's view. The addSubView for the subVC will be breaking the auto layout constraints within your navigation controller.

Change your calls to match the following and see what results you get:

UINavigationController * childNaviVC = [[UINavigationController alloc] initWithRootViewController:self.subVC];
[self addChildViewController:childNaviVC];
[self.view addSubview:childNaviVC.view];
[childNaviVC.view setFrame:self.view.bounds];
[childNaviVC didMoveToParentViewController:self];
Matthew Cawley
  • 2,828
  • 1
  • 31
  • 42
-1

Add the function viewDidLayoutSubviews to children VC ,and it worked. -(void)viewDidLayoutSubviews{

    self.view.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}
Snail
  • 3,046
  • 1
  • 9
  • 12