建立父子关系后,容器设置子视图控制器的视图大小,并且把它添加到自己的视图层级关系中。设定子视图控制器的视图大小是很重要的,确保视图正确显示在父控制器的视图中。添加视图后,父控制器调用子视图控制器的didMoveToParentViewController:方法,父视图控制器给孩子一个机会来响应视图所属关系的改变。
//添加子控制器
- (void)displayContentController:(UIViewController*)content contentFrame:(CGRect)frame {
[self addChildViewController:content];
content.view.frame = frame;
[self.view addSubview:content.view];
[content didMoveToParentViewController:self];
}
//移除子控制器
- (void) hideContentController: (UIViewController*) content {
[content willMoveToParentViewController:nil];
[content.view removeFromSuperview];
[content removeFromParentViewController];
}
//子控制器之间的切换
- (void)cycleFromViewController: (UIViewController*) oldVC
toViewController: (UIViewController*) newVC {
// Prepare the two view controllers for the change.
[oldVC willMoveToParentViewController:nil];
[self addChildViewController:newVC];
// Get the start frame of the new view controller and the end frame
// for the old view controller. Both rectangles are offscreen.
newVC.view.frame = CGRectMake(100, 200, 100, 100);
CGRect endFrame = CGRectMake(200, 100, 100, 100);
// Queue up the transition animation.
[self transitionFromViewController: oldVC toViewController: newVC
duration: 1.25 options:UIViewAnimationOptionCurveEaseIn
animations:^{
// Animate the views to their final positions.
newVC.view.frame = oldVC.view.frame;
oldVC.view.frame = endFrame;
}
completion:^(BOOL finished) {
// Remove the old view controller and send the final
// notification to the new view controller.
[oldVC removeFromParentViewController];
[newVC didMoveToParentViewController:self];
}];
}
网友评论