Im writing a custom view controller container as per the iOS documentation on view controllers and i’m struggling on finding an elegant way to present the first view controller that also forwards the relevant display messages like viewWillAppear: automatically.
When i try transitionFromViewController:toViewController:duration:options:animations:completion: with the fromViewController: as nil i get an error. I have resorted to animating the view into the view hierarchy with a UIView animation block. This seems to break the automatic forwarding of the appearance methods and means its my responsibility to call viewWillAppear: and ViewDidAppear: at the appropriate times. Is there a more efficient way to transition the first view on to the screen that takes care of the appearance and rotation methods?
My code looks a little like this for animating in the first view controller.
self.visibleViewController = [[UIViewController alloc] init];
[self addChildViewController:self.visibleViewController];
[self.visibleViewController viewWillAppear:YES];
[self.visibleViewController.view setAlpha:0];
[self.view addSubview:self.visibleViewController.view];
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
self.visibleViewController.view.alpha = 1;
}
completion:^(BOOL finished){
[self.visibleViewController viewDidAppear];
[self.visibleViewController didMoveToParentViewController:self];
}];
The answer was right there hidden in the documentation all along.
The documentation for
UIViewControlleris- (void)beginAppearanceTransition:(BOOL)isAppearing animated:(BOOL)animatedand the companion
- (void)endAppearanceTransitionTheir documentation says never to call
viewWillAppearand such from your own code. You can kick the chain off properly with these methods.